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

177
Visualizações
Why can't the compiler deduce the type of an inline lambda parameter to Stream.of?

Why does Stream.of() require a cast to take an inline lambda expression or method reference?

Consider this:

 Function<String,String> f = String::toUpperCase; // works without cast
 Stream.of(f); // works without cast
 //Stream.of(String::toUpperCase); // Error - target type must be functional interface
 Stream.of((Function<String,String>) String::toUpperCase); //OK

The assignment to the variable f does not require a cast, but when used as an inline parameter to Stream.of a cast is required. Why?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

A lambda or method reference by itself doesn't have a type, it derives its type from the context (e.g. the variable it's assigned to), or in other words it's contextually typed. When you use Stream.of(...) without further context to infer the type (e.g. returning, so the type is specified by the return type, or assigning to a variable or parameter, or using explicit generic parameters), there is no type information available to construct the lambda or method reference.

The reason is that Java doesn't know whether you want a Function<String, String>, or a UnaryOperator<String>, or some other functional interface with a compatible signature.

You need to do something like:

public Stream<Function<String, String>> example1() {
    return Stream.of(String::toUpperCase);
}

public void example2() {
    Stream<Function<String, String>> stream = Stream.of(String::toUpperCase);
}

public void example3() {
    Stream.<Function<String, String>>of(String::toUpperCase);
}

public void example4() {
    doSomething(Stream.of(String::toUpperCase));
}

private void doSomething(Stream<Function<String, String>> stream) {
    // ...
}

See also the Java Tutorial on lambdas, specifically section Target Typing:

[...] The data type that these methods expect is called the target type. To determine the type of a lambda expression, the Java compiler uses the target type of the context or situation in which the lambda expression was found. It follows that you can only use lambda expressions in situations in which the Java compiler can determine a target type:

  • Variable declarations
  • Assignments
  • Return statements
  • Array initializers
  • Method or constructor arguments
  • Lambda expression bodies
  • Conditional expressions, ?:
  • Cast expressions
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