Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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

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?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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

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

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

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

You would have the same error with this simplified code:

IntStream.empty().flatMap(x -> Stream.of(LongStream.empty()));
over 4 years ago · Santiago Trujillo Report

0

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.

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.

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);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!