Eclipse support for new Java 8 language features such as lambdas is not quite there yet.
If you want to play with new Java 8 language features but don't want to switch to another IDE such as IntelliJ IDEA then you can use a development build of the upcoming Eclipse Luna and set up an ant script to use javac from JDK8 as your compiler instead of the built-in EJC from Eclipse (which doesn't seem to support all the Java 8 features yet).
Get your JDK8 early access release here:
Get Eclipse Luna here:
Set your Project Properties->Compiler Compliance Level to 1.8 BETA
Use an ant script like this one to do your compiling (create as build.xml at the same directory level as your src folder)
<project name="lambdas" default="all" basedir=".">
<property name="src" value="src" />
<property name="bin" value="bin" />
<property name="lib" value="lib" />
<target name="init">
<delete dir="${bin}" />
<mkdir dir="${bin}" />
</target>
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${bin}" />
</target>
<target name="all" depends="compile" />
</project>
<property name="src" value="src" />
<property name="bin" value="bin" />
<property name="lib" value="lib" />
<target name="init">
<delete dir="${bin}" />
<mkdir dir="${bin}" />
</target>
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${bin}" />
</target>
<target name="all" depends="compile" />
</project>
In your Ant Build settings ("Main" tab) use the arguments
-Dbuild.compiler=modern
Try a little Java 8 lambda test such as:
public class Test
{
interface Operation
{
int op(int a, int b);
}
public static int doOp(int a, int b, Operation theOp)
{
return theOp.op(a, b);
}
public static void main(String[] args)
{
Operation add = (a, b) -> a + b;
Operation sub = (a, b) -> a - b;
System.out.println(doOp(3, 4, add));
System.out.println(doOp(5, 4, sub));
}
}
{
interface Operation
{
int op(int a, int b);
}
public static int doOp(int a, int b, Operation theOp)
{
return theOp.op(a, b);
}
public static void main(String[] args)
{
Operation add = (a, b) -> a + b;
Operation sub = (a, b) -> a - b;
System.out.println(doOp(3, 4, add));
System.out.println(doOp(5, 4, sub));
}
}