Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

480
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda