-1

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

Vipin
  • 4,851
  • 3
  • 35
  • 65

2 Answers2

1

As there are 2 return statements, you can either have the braces as you posted above:

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

Or you need to remove the other return:

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

For further knowledge, you can visit the following link:

https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#syntax

Brijesh Shah
  • 172
  • 8
1

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

sourabh1024
  • 647
  • 5
  • 15