Skip to content

Instantly share code, notes, and snippets.

View irbull's full-sized avatar

Ian Bull irbull

View GitHub Profile
MyInterface r2 = () ->
System.out.println("Hello, world");
System.out.println("Goodbye, world!");
vs.
MyInterface r2 = () -> {
System.out.println("Hello, world");
System.out.println("Goodbye, world!");
};
@irbull
irbull / gist:9770904
Created March 25, 2014 20:43
Java 8 Lambda example
In this simple example, we are taking a List<String> of names, sorting them, creating Person objects and printing the result. The first way uses single line lambda expressions:
names.stream().sorted((o1, o2) -> o1.compareTo(o2)).
map(name -> new Person(name)).
forEach(p -> System.out.println(p.getName()));
The second one wraps each expression in a bracket and makes the types explicit:
names.stream().sorted(( String o1, String o2 ) ->
{