2. * Brief history of Java
*What's new in Java 8
*Why Lambda?
* Sample Program &&
Bench mark
James Gosling: Father of Java
3. Date Version Highlights
1991 Oak by James Gosling@Sun
Microsystems
Named after oak tree stood
outside his office. For small
PDA device. Slight flavor of C
1996 Jan 23 JDK 1.0 “Oak” sounds similar to “awk”?
1997 Java 1.1 JDBC, reflection, JIT compiler
1998 Java2 (J2SE) Collection framework,
Swing(GUI)API
2000 Java 1.3 JNDI, RMI
2002 Feb. Java 1.4 Regex, JAXP
2004 Sept. Java 1.5 Generics, annotation, enum,
concurrency, varargs
2006 Dec. Java 1.6 Performance tune, JDBC4
2011 July Java 1.7 JVM support dynamic language
4. Features
DateTime API (no more new Date(), Calendar.getInstance()!)
Nashborn JavaScript engine ($JAVA_HOME/bin/jjs)
Default method in Interface / Improved concurrent package /
Optional
Lambda expressions
@FunctionalInterface annotations
Method Reference
java.util.Stream package
Backward Compatibility
Binary compatibility && Behavioral compatibility - Excellent
Source compatibility - Good
5. Lambda is a syntax that produces instance of
Functional Interface.
6. Functional Interface is:
Interface that has exactly one abstract method
- Runnable ( run() )
- Comparator (compare() )
See
@FunctionalInterface (java.lang.annotation)
List of interface under java.util.function package
You can define your own interface, of course.
7. Leap to functional language (Haskell, Erlang,
JavaScript, Lisp, Scala, Ocaml, etc.)
Single-method interface (syntax sugar)
Provide a path to multicore (stream vs.
parallel stream) -> see demo.
No more for loop (internal loop vs. external
loop)
8. In short … function is the 1st level Object
/*
* JavaScript can pass method as a parameter.
*/
var doSomething = function(name){
console.log(name + "is going to do something someday");
}
function useSomething(func, param){
func(param);
}
Event handler
9. (parameter) -> { // do something} ; // that’s it!
Further simplify with “Type interface” - best guess
@FunctionalInterface
private interface HelloWorld{
public String hello(String name);
}
Old anonymous class
HelloWorld hello = new HelloWorld() {
@Override
public String hello(String name) {
return "Hello, " + name;
}
};
Ex 1) -> Get Simplified with Lambda (You don’t need to specify method name)
(String name) -> { return “Hello “+name + “!!”; };
Ex 2) Remove String -> JVM best guess – type interface
(name) -> { return “Hello “+name+”!!”; };
Ex 3) Remove return – type interface
(name) -> { “Hello “+name+”!!”; };
Ex 4) Remove brackets for single implementation.
(name) -> “Hello “+name+”!!”;
Ex 5) Remove () if parameter is one
name -> “Hello “+name+”!!”;
(name -> “Hello “+name+”!!”;
10. Start with .stream(), chain with intermediate
operators, end with terminal operator.
IntermediateOperator
(filter, map, sorted, distinct, limit)
Terminal Operator
(forEach, collect, reduce, iterator)
11. listOfSomething.stream()
.filter( element -> element.age > 30) //
.map( ) // not evaluated
.sorted() // not evaluated
.limit() // not evaluated
….
.collect( Collectors.toList() ); Finally evaluate
12. Sample Code (Run com.hidetomo.Java8StreamBench.main)
https://github.com/mocchidesu/Java8sample.git
Input: 100,000 last name from HSA
Do:
Filter: Last name start with ‘m’
Map: (Brace each name with [] bracket)
Sort: Reverse order
Dedupe
Limit to top 2 results
Loop 1000 times.
Traditional Java7 For Loop approach vs. Sequential
Lambda vs. Parallel Lambda. -> IntelliJ13
13. Java 8 is backward compatible in binary level
Lambda is not only a syntax sugar
It’s a new era of Java
Performance gain?Will be improved. Stay
tuned.
When use? NOW
14. Everything about Java 8
http://www.techempower.com/blog/2013/03/26/everything-about-java-8/
Oracle Compatibility Guide for JDK 8
http://www.oracle.com/technetwork/java/javase/8-compatibility-guide-
2156366.html
Lambda Peek under the Hood by Brian Goetz - Java One 2013
http://www.slideshare.net/jaxlondon2012/lambda-a-peek-under-the-hood-brian-
goetz
10 Example of Lambda Expressions and Streams in Java 8
http://javarevisited.blogspot.com/2014/02/10-example-of-lambda-expressions-in-
java8.html