KEMBAR78
Java8 | PDF
first taste of Java 8 
Sunil Kumar | Wiley London | 22-01-2014
Java 8 features 
● Lambda 
● Interface improvement 
● Streams 
● new packages 
Keywords : Big data, Parallelism, Cloud computing
1. Lambda (anonymous function) 
In mathematics and computing generally, a lambda expression is a function: for some 
or all combinations of input values it specifies an output value. Lambda expressions in 
Java introduce the idea of functions into the language 
Ex: 0 param 
Runnable r = () -> System.out.println("hello world"); 
r.run(); // prints "hello world" 
Object o = () -> System.out.println("hello world"); //won’t compile 
Ex: 1 param 
FileFilter filter = f -> f.getAbsolutePath().endsWith(".txt"); 
return filter.accept(new File("myfile.txt")); // returns true
Lambda - syntax 
Lambda expression has three part : parenthesized params, arrow and body 
● (parameters) -> expression 
● (parameters) -> {statements…} 
Type inference 
Comparator<String> c = (lhs, rhs) -> { 
System.out.println("comparing : " + lhs + " to " + rhs); 
return lhs.compareTo(rhs); 
}; 
int result = c.compare(“hello”, “world”)
More examples 
List<Apple> inventory = Arrays.asList(new Apple(80,"green"), 
new Apple(155, "green"), 
new Apple(120, "red")); 
// [Apple{color='green', weight=80}, Apple{color='green', weight=155}] 
List<Apple> greenApples2 = filterApples(inventory, (Apple a) -> "green".equals(a.getColor())); 
System.out.println(greenApples2); 
// [Apple{color='green', weight=155}] 
List<Apple> heavyApples2 = filterApples(inventory, (Apple a) -> a.getWeight() > 150); 
System.out.println(heavyApples2); 
public static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p){ 
List<Apple> result = new ArrayList<>(); 
for(Apple apple : inventory){ 
if(p.test(apple)){ 
result.add(apple); 
}} 
return result; 
}
Method References 
Method references are compact lambda expressions for methods that already have a 
name. 
● String::valueOf x -> String.valueOf(x) 
● Object::toString x -> x.toString() 
● x::toString () -> x.toString() 
● ArrayList::new () -> new ArrayList<>()
2. Interface improvement 
(Functional interface) 
A functional interface is one whose type can be used for a method parameter when a lambda is to 
be supplied as the actual argument. 
For example, the forEach method on collections could have the following signature: 
public void forEach(Consumer<? super T> consumer); 
More precisely, a functional interface is defined as any interface that has exactly one explicitly 
declared abstract method. 
● interface can now define static methods. 
● interfaces can now provide default methods 
(The qualification is necessary because an interface may have non-abstract default methods.) This 
is why functional interfaces used to be called Single Abstract Method (SAM) interfaces, a term 
that is still sometimes seen.
3. Streams 
A stream is a sequence of values. Allows functional-style operations on streams of 
values. 
Stream<T> stream = collection.stream(); 
Stream<T> stream = collection.parallelStream();
Steams type 
Operations Interface Used lambda signature return type return value 
Sample lazy/intermediate operations 
filter Predicate<T> T -> boolean Stream<T> stream containing input 
elements that satisfy the 
Predicate 
map Function<T, R> T -> R Stream<R> stream of values, the result 
of applying the Function to 
each input element 
sorted Comparator<T> (T, T) -> int Stream<T> stream containing the input 
elements, sorted by the 
Comparator 
Sample eagar/terminal operation 
reduce BinaryOperator<T> (T, T) -> T Optional<T> 
forEach Consumer<T> T -> void void
4. java.util.function 
Function<T, R> - take T as input, return R as output 
Predicate<T> - take T as input, return a boolean as output 
Consumer<T> - take T as input, don't return anything 
Supplier<T> - take no input, return T 
BinaryOperator<T> - take two T's as input, return one T as output
Thank You. 
Q & A.
Reference: 
● http://www.lambdafaq.org/ 
● http://www.infoq.com/news/2013/08/everything-about-java-8 
● https://blogs.oracle.com/thejavatutorials/entry/jdk_8_documentation_developer_preview 
● http://sett.ociweb.com/sett/settFeb2013.html 
● http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html 
● https://github.com/java8/LambdasInAction

Java8

  • 1.
    first taste ofJava 8 Sunil Kumar | Wiley London | 22-01-2014
  • 2.
    Java 8 features ● Lambda ● Interface improvement ● Streams ● new packages Keywords : Big data, Parallelism, Cloud computing
  • 3.
    1. Lambda (anonymousfunction) In mathematics and computing generally, a lambda expression is a function: for some or all combinations of input values it specifies an output value. Lambda expressions in Java introduce the idea of functions into the language Ex: 0 param Runnable r = () -> System.out.println("hello world"); r.run(); // prints "hello world" Object o = () -> System.out.println("hello world"); //won’t compile Ex: 1 param FileFilter filter = f -> f.getAbsolutePath().endsWith(".txt"); return filter.accept(new File("myfile.txt")); // returns true
  • 4.
    Lambda - syntax Lambda expression has three part : parenthesized params, arrow and body ● (parameters) -> expression ● (parameters) -> {statements…} Type inference Comparator<String> c = (lhs, rhs) -> { System.out.println("comparing : " + lhs + " to " + rhs); return lhs.compareTo(rhs); }; int result = c.compare(“hello”, “world”)
  • 5.
    More examples List<Apple>inventory = Arrays.asList(new Apple(80,"green"), new Apple(155, "green"), new Apple(120, "red")); // [Apple{color='green', weight=80}, Apple{color='green', weight=155}] List<Apple> greenApples2 = filterApples(inventory, (Apple a) -> "green".equals(a.getColor())); System.out.println(greenApples2); // [Apple{color='green', weight=155}] List<Apple> heavyApples2 = filterApples(inventory, (Apple a) -> a.getWeight() > 150); System.out.println(heavyApples2); public static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p){ List<Apple> result = new ArrayList<>(); for(Apple apple : inventory){ if(p.test(apple)){ result.add(apple); }} return result; }
  • 6.
    Method References Methodreferences are compact lambda expressions for methods that already have a name. ● String::valueOf x -> String.valueOf(x) ● Object::toString x -> x.toString() ● x::toString () -> x.toString() ● ArrayList::new () -> new ArrayList<>()
  • 7.
    2. Interface improvement (Functional interface) A functional interface is one whose type can be used for a method parameter when a lambda is to be supplied as the actual argument. For example, the forEach method on collections could have the following signature: public void forEach(Consumer<? super T> consumer); More precisely, a functional interface is defined as any interface that has exactly one explicitly declared abstract method. ● interface can now define static methods. ● interfaces can now provide default methods (The qualification is necessary because an interface may have non-abstract default methods.) This is why functional interfaces used to be called Single Abstract Method (SAM) interfaces, a term that is still sometimes seen.
  • 8.
    3. Streams Astream is a sequence of values. Allows functional-style operations on streams of values. Stream<T> stream = collection.stream(); Stream<T> stream = collection.parallelStream();
  • 9.
    Steams type OperationsInterface Used lambda signature return type return value Sample lazy/intermediate operations filter Predicate<T> T -> boolean Stream<T> stream containing input elements that satisfy the Predicate map Function<T, R> T -> R Stream<R> stream of values, the result of applying the Function to each input element sorted Comparator<T> (T, T) -> int Stream<T> stream containing the input elements, sorted by the Comparator Sample eagar/terminal operation reduce BinaryOperator<T> (T, T) -> T Optional<T> forEach Consumer<T> T -> void void
  • 10.
    4. java.util.function Function<T,R> - take T as input, return R as output Predicate<T> - take T as input, return a boolean as output Consumer<T> - take T as input, don't return anything Supplier<T> - take no input, return T BinaryOperator<T> - take two T's as input, return one T as output
  • 11.
  • 12.
    Reference: ● http://www.lambdafaq.org/ ● http://www.infoq.com/news/2013/08/everything-about-java-8 ● https://blogs.oracle.com/thejavatutorials/entry/jdk_8_documentation_developer_preview ● http://sett.ociweb.com/sett/settFeb2013.html ● http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html ● https://github.com/java8/LambdasInAction