Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

486
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda