Just playing with the new lambda and functional features in Java 8 and I'm not sure how to do this.
For example the following is valid:
Map<String, Integer> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
map.compute("A", (k, v) -> v == null ? 42 : v + 41));
but the following gives me syntax errors:
BiFunction x = (k, v) -> v == null ? 42 : v + 41;
map.compute("A", x);
Any ideas?
example of passing a lambda containing a method
You have forgotten the generics on your
BiFunction
:Running:
As Boris The Spider points out, the specific problem you have is the generics. Depending on context, adding an explicit {} block around the lambda will help add clarity and may be required.
With that, you would get something like:
It would be pretty helpful for future readers who have the same problem if you posted your syntax errors so they can be indexed.
This link has some additional examples that might help.