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

时间:2021-02-21 17:01:00

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表达式中不存在的错误返回类型,不存在类型变量(s),因此java.util.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(..)之间的区别是什么?有没有办法消除额外的平面图电话?

2 个解决方案

#1


5  

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

. maptoobj (..). flatmap(..)和. flatmap(..)期待完全不同的签名。

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

.mapToObj(..).flatMap(..)期望一个int ->对象函数和一个对象->流 函数。

.flatMap(..) expects an int -> IntStream function.

. flatmap(..)期望int -> IntStream函数。

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

如果您破坏了代码,您将传递一个int ->流 函数,它与int -> IntStream函数不兼容。

You would have the same error with this simplified code:

你会有同样的错误,用这个简化的代码:

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

#2


1  

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 int in our stream to a LongStream. If you intend for it to be a single LongStream, you need to do a flatMapToLong.

在第2行上的lambda不返回一个LongStream,而是一个流 ,因为我们正在将流中的每个int转换成一个LongStream。如果你想让它成为一个单一的长流,你需要做一个flatMapToLong。

The flatMap on line 3 expects an int -> int function, which yours is not. However, you can use mapToObj instead, which takes the method that you're providing it.

第3行的flatMap期望一个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);

#1


5  

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

. maptoobj (..). flatmap(..)和. flatmap(..)期待完全不同的签名。

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

.mapToObj(..).flatMap(..)期望一个int ->对象函数和一个对象->流 函数。

.flatMap(..) expects an int -> IntStream function.

. flatmap(..)期望int -> IntStream函数。

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

如果您破坏了代码,您将传递一个int ->流 函数,它与int -> IntStream函数不兼容。

You would have the same error with this simplified code:

你会有同样的错误,用这个简化的代码:

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

#2


1  

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 int in our stream to a LongStream. If you intend for it to be a single LongStream, you need to do a flatMapToLong.

在第2行上的lambda不返回一个LongStream,而是一个流 ,因为我们正在将流中的每个int转换成一个LongStream。如果你想让它成为一个单一的长流,你需要做一个flatMapToLong。

The flatMap on line 3 expects an int -> int function, which yours is not. However, you can use mapToObj instead, which takes the method that you're providing it.

第3行的flatMap期望一个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);