-
-
Save blabadi/b38d4186efe80681b7ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.blabad.jdk8.lambda; | |
import java.util.Arrays; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.function.Consumer; | |
public class Driver { | |
public static void main(String[] args) { | |
System.out.println("=======test 1======="); | |
List<String> names = Arrays.asList("F", "Fo", "Foo"); | |
Iterator<Integer> lengths = names.stream().map(name -> name.length()).iterator(); | |
lengths.forEachRemaining(x -> { | |
System.out.println(x); | |
System.out.println("---"); | |
}); | |
// or using more java 8 features : | |
//method reference | |
System.out.println("========test 2========"); | |
names.stream().map(String::length).forEach(System.out::println); | |
//lambda expression + functional Interface | |
System.out.println("========test 3========"); | |
Consumer<String> myPrint = (x) -> { | |
System.out.println(x); | |
System.out.println("---"); | |
}; | |
names.stream().filter(x -> x.length() > 2).forEach(myPrint);; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment