Saturday, April 21, 2012

java 1.8 lambda example


I saw at Devoxx France, a conference about java 1.8 and the use of lambdas by Guillaume Tardif. (http://www.devoxx.com/display/FR12/JDK+8+demo++lambdas+in+Action)

In this article, I will show you an example.You can dowload the jdk here : http://jdk8.java.net/lambda/

To install jdk1.8 on Ubunutu, you can do :



sudo mv ./jdk1.8.0 /usr/lib/jvm/
sudo update-alternatives --install “/usr/bin/java” “java” “/usr/lib/jvm/jdk1.8/bin/java” 1
sudo update-alternatives --install “/usr/bin/javac” “javac” “/usr/lib/jvm/jdk1.8/bin/javac” 1

sudo update-alternatives --config java


Then, you just have to choose the jdk.


Note : I have a bug when I use javac because javac -version show me always 1.6 and not 1.8
So to compile, I've done the following :

/usr/lib/jvm/jdk1.8.0/bin/javac Main.java
java Main


Here is my file Main.java:

import java.util.ArrayList;
import java.util.List;


public class Main {

public static void main(String[] args) {
List names = new ArrayList();
names.add("superman");
names.add("batman");
names.forEach( (String s) -> { System.out.println(s);} );
}

}


Then we show on the screen :

superman
batman


An other important point, Java 1.8 should support multiple inheritance !
If you have comment, post it !

No comments:

Post a Comment