java 编译时间:不存在类型变量 U 的实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42801892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:53:03  来源:igfitidea点击:

compile time: no instance(s) of type variable(s) U exist

javajava-8java-stream

提问by Andreas

The following statement, although nonsensical, appears syntactically sound.

下面的语句虽然荒谬,但在语法上看起来很合理。

final Stream<LongStream> foobar = IntStream.empty()
    .flatMap(x -> IntStream.empty()
        .mapToObj(y -> IntStream.empty()
            .mapToLong(z -> 1))); //compilation error here on `z -> 1`

However it does not compile, returning:

但是它不会编译,返回:

java: incompatible types: bad return type in lambda expression no instance(s) of type variable(s) U exist so that java.util.stream.Stream conforms to java.util.stream.IntStream

java:不兼容的类型:lambda 表达式中的错误返回类型不存在类型变量 U 的实例,因此 java.util.stream.Stream 符合 java.util.stream.IntStream

However if you delay the flatmap, everything works fine:

但是,如果您延迟平面图,则一切正常:

final Stream<LongStream> foobar = IntStream.empty()
    .mapToObj(x -> IntStream.empty()
        .mapToObj(y -> IntStream.empty()
            .mapToLong(z -> 1)))
    .flatMap(x -> x);

What is the difference between .mapToObj(..).flatMap(..)and just .flatMap(..)? Is there someway to eliminate the extra flatmap call?

.mapToObj(..).flatMap(..)和 just 和有什么不一样.flatMap(..)?有没有办法消除额外的平面图调用?

采纳答案by 4castle

.mapToObj(..).flatMap(..)and .flatMap(..)expect completely different signatures.

.mapToObj(..).flatMap(..).flatMap(..)期待完全不同的签名。

.mapToObj(..).flatMap(..)expects an int -> Objectfunction, and an Object -> Stream<?>function.

.mapToObj(..).flatMap(..)期望一个int -> Object函数,一个Object -> Stream<?>函数。

.flatMap(..)expects an int -> IntStreamfunction.

.flatMap(..)期待一个int -> IntStream功能。

If you break down your code, you're passing an int -> Stream<LongStream>function, which isn't compatible with an int -> IntStreamfunction.

如果你分解你的代码,你正在传递一个int -> Stream<LongStream>int -> IntStream函数不兼容的函数。

You would have the same errorwith this simplified code:

使用此简化代码,您会遇到相同的错误

IntStream.empty().flatMap(x -> Stream.of(LongStream.empty()));

回答by Joe C

I've refactored your method to break down what it's doing:

我重构了你的方法来分解它在做什么:

IntFunction<LongStream> f1 = y -> IntStream.empty().mapToLong(z -> 1);
IntFunction<LongStream> f2 = x -> IntStream.empty().mapToObj(f1);
final Stream<LongStream> foobar = IntStream.empty().flatMap(f2);

We have two things wrong here:

我们这里有两个错误:

The lambda on line 2 does not return a LongStream, but rather a Stream<LongStream>, as we are converting each intin our stream to a LongStream. If you intend for it to be a single LongStream, you need to do a flatMapToLong.

第 2 行的 lambda 不返回 a LongStream,而是返回 a Stream<LongStream>,因为我们将int流中的每个都转换为 a LongStream。如果你打算让它成为一个单一的LongStream,你需要做一个flatMapToLong.

The flatMapon line 3 expects an int -> intfunction, which yours is not. However, you can use mapToObjinstead, which takes the method that you're providing it.

flatMap3 行需要一个int -> int函数,而你的不是。但是,您可以mapToObj改用它,它采用您提供的方法。

So the corrected method would be:

所以更正的方法是:

IntFunction<LongStream> f1 = y -> IntStream.empty().mapToLong(z -> 1);
IntFunction<LongStream> f2 = x -> IntStream.empty().mapToObj(f1).flatMapToLong(i -> i);
final Stream<LongStream> foobar = IntStream.empty().mapToObj(f2);