My First Experience with Lambda
      Expressions in Java 8


  Tibor Digana
What do I need ?
• First of all download and install OpenJDK 1.8
http://jdk8.java.net/lambda/
• Write and compile the following simple code
Threads and For-Each in Lambda
Overriding run method
• Runnable#run() is anonymous function
• New Thread instance is created
• New Runnable instance is passed to the
  constructor of Thread
• run() method calls connectToService()
forEach on Collection
•   Anonymous function with one parameter
•   for-each iterates over given List
•   Takes elements and computes their sum
•   The sum variable must be final or static
Map-Reduce
List<Integer> list = Arrays.asList(1, 2, 3);
int x = list.stream().map(e -> e).reduce(0, (a, b) -> a + b);

•   Mapping values of elements 1:1
•   Splitting the collection to two parties
•   Splitting parties again, etc.
•   Computing the SUM on each party
•   Reducing their sub-results in the final result
Anonymous Function without
     parameters is Runnable Object
run(Runnable... blocks) {…}
……
run( () -> { System.out.println("first..."); },
     () -> { System.out.println("second..."); }
);
•   Passing functionality/task to a thread pool
•   Simple when assigning Runnable r = () -> {…};
•   Simply registering a listener #addListener( ()-> {…} )
•   No need to write anonymous class with one method
References
•   http://stackoverflow.com/questions/10332402/cant-get-lambdas-to-compile-in-netbeans-7-1-1jdk8-jdk-8-ea-bin-b35
•   http://jdk8.java.net/lambda/
•   http://www.javabeat.net/2012/05/enhanced-collections-api-in-java-8-supports-lambda-expressions/
•   http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-4.html

My first experience with lambda expressions in java

  • 1.
    My First Experiencewith Lambda Expressions in Java 8 Tibor Digana
  • 2.
    What do Ineed ? • First of all download and install OpenJDK 1.8 http://jdk8.java.net/lambda/ • Write and compile the following simple code
  • 3.
  • 4.
    Overriding run method •Runnable#run() is anonymous function • New Thread instance is created • New Runnable instance is passed to the constructor of Thread • run() method calls connectToService()
  • 5.
    forEach on Collection • Anonymous function with one parameter • for-each iterates over given List • Takes elements and computes their sum • The sum variable must be final or static
  • 6.
    Map-Reduce List<Integer> list =Arrays.asList(1, 2, 3); int x = list.stream().map(e -> e).reduce(0, (a, b) -> a + b); • Mapping values of elements 1:1 • Splitting the collection to two parties • Splitting parties again, etc. • Computing the SUM on each party • Reducing their sub-results in the final result
  • 7.
    Anonymous Function without parameters is Runnable Object run(Runnable... blocks) {…} …… run( () -> { System.out.println("first..."); }, () -> { System.out.println("second..."); } ); • Passing functionality/task to a thread pool • Simple when assigning Runnable r = () -> {…}; • Simply registering a listener #addListener( ()-> {…} ) • No need to write anonymous class with one method
  • 8.
    References • http://stackoverflow.com/questions/10332402/cant-get-lambdas-to-compile-in-netbeans-7-1-1jdk8-jdk-8-ea-bin-b35 • http://jdk8.java.net/lambda/ • http://www.javabeat.net/2012/05/enhanced-collections-api-in-java-8-supports-lambda-expressions/ • http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-4.html