Search code examples
javalambdaconcurrenthashmap

Iambda expression compilation error


I am using search of ConcurrentHashMap like this:

map.search(1, (k, v) -> {  
  return v.size() > 10 ? k : null;
});

But when I remove braces it gives me compilation error:

map.search(1, (k, v) -> 
  return v.size() > 10 ? return k : null;
);

I want to remove braces since it is single statement in lambda expression.

Update : corrected typo


Solution

  • A return statement is not an expression , its a statement. In lambda expression you must enclose statement in {} braces. For further details you can study here : https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#syntax