Skip to content

Instantly share code, notes, and snippets.

@blabadi
Created Sep 8, 2014
Embed
What would you like to do?
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