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

173
Views
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 answers
Answer question

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 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!