• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
My first experience with lambda expressions in java
 

My first experience with lambda expressions in java

on

  • 1,442 views

 

Statistics

Views

Total Views
1,442
Views on SlideShare
1,437
Embed Views
5

Actions

Likes
0
Downloads
13
Comments
0

1 Embed 5

http://www.linkedin.com 5

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    My first experience with lambda expressions in java My first experience with lambda expressions in java Presentation Transcript

    • 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 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-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
    • 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
    • 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