I’m playing with future java 8 release aka JDK 1.8.
And I found out that you can easily do
interface Foo { int method(); }
and use it like
Foo foo = () -> 3;
System.out.println("foo.method(); = " + foo.method());
which simply prints 3.
And I also found that there is a java.util.function.Function interface which does this in a more generic fashion. However this code won’t compile
Function times3 = (Integer triple) -> 3 * triple;
Integer twelve = times3.map(4);
And it seems that I first have to do something like
interface IntIntFunction extends Function<Integer, Integer> {}
IntIntFunction times3 = (Integer triple) -> 3 * triple;
Integer twelve = times3.map(4);
So I’m wondering if there is another way to avoid the IntIntFunction step?
@joop and @edwin thanks.
Based on latest release of JDK 8 this should do it.
And in case you do not like you can make it a bit more smooth with something like
So you do not need to specify a type or parentheses but you’ll need to cast the parameter when you access it.