My First Experience with Lambda      Expressions in Java 8  Tibor Digana
What do I need ?• First of all download and install OpenJDK 1.8http://jdk8.java.net/lambda/• Write and compile the followi...
Threads and For-Each in Lambda
Overriding run method• Runnable#run() is anonymous function• New Thread instance is created• New Runnable instance is pass...
forEach on Collection•   Anonymous function with one parameter•   for-each iterates over given List•   Takes elements and ...
Map-ReduceList<Integer> list = Arrays.asList(1, 2, 3);int x = list.stream().map(e -> e).reduce(0, (a, b) -> a + b);•   Map...
Anonymous Function without     parameters is Runnable Objectrun(Runnable... blocks) {…}……run( () -> { System.out.println("...
References•   http://stackoverflow.com/questions/10332402/cant-get-lambdas-to-compile-in-netbeans-7-1-1jdk8-jdk-8-ea-bin-b...
Upcoming SlideShare
Loading in …5
×

My first experience with lambda expressions in java

1,606 views

Published on

Published in: Technology, News & Politics
  • Be the first to comment

  • Be the first to like this

My first experience with lambda expressions in java

  1. 1. My First Experience with Lambda Expressions in Java 8 Tibor Digana
  2. 2. What do I need ?• First of all download and install OpenJDK 1.8http://jdk8.java.net/lambda/• Write and compile the following simple code
  3. 3. Threads and For-Each in Lambda
  4. 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. 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. 6. Map-ReduceList<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. 7. Anonymous Function without parameters is Runnable Objectrun(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. 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

×