180 likes | 380 Views
JDK 8. Introduction to JDK 8. Agenda. JDK 8 - Composition. Second part of Plan B. Lambda. Project Jigsaw. Project Coin. ++. ++. Lambda Expressions. An anonymous function with argument list, return type and a body. (argument list) arrow_operator { java_statements }
E N D
JDK 8 Introduction to JDK 8
JDK 8 - Composition Second part of Plan B Lambda Project Jigsaw Project Coin ++ ++
Lambda Expressions An anonymous function with argument list, return type and a body. (argument list) arrow_operator { java_statements} (int, int) -> { x+y} [ ] A definitive epitome programming style dating back to lisp style programming. [ ] Instances of Functional Interfaces. [ ] Lexically scoped programming style.
java.util.function java.lang.FunctionalInterface Function Predicate Consumer IntToLongFunction BooleanSupplier … … A programming paradigm Also called, SAM (Single Abstract Method)
Usage of Lambda Expressions Runnable runnable = () -> { System.out.println("Richard Feynman !!! "); }; runnable.run(); interface java.lang.Runnable { void run(); } interface java.lang.Iterable<T> { java.util.Iterator<T> iterator(); } interfacejava.util.concurrent.Executor{ voidexecute(java.lang.Runnable command); } interfacejava.awt.event.ActionListener{ voidactionPerformed(java.awt.event.ActionEvent e); } Now a functional interface !!!
Internal Iteration Use the forEach List listOfStrings = new ArrayList<String>(); listOfStrings.add("alan"); listOfStrings.add("mathison"); listOfStrings.add("turing"); listOfStrings.forEach(x -> System.out.println(x));
API’s java.util.Optional<T> Optional<Chola> op = Optional.empty(); Optional<Chola> op = Optional.of(new Chola("Karikala")); op.isPresent(); Chola s = op.orElse(new Chola("Raja Raja")); s.getName(); class Chola extends King { String name = "default"; public Chola(String _name) { name = _name; } String getName() { return name; } }
API’s Arrays.asList(stringArray).stream() .filter(s -> s.startsWith("1")) .forEach( s -> { System.out.println(s); }); Arrays.asList(stringArray).parallelStream() .filter(s -> s.startsWith("2")) .forEach( s -> { System.out.println(s); }); Arrays.asList(stringArray).stream() .distinct() .forEach( s -> { System.out.println(s); });
API’s Java.util.StringJoiner StringJoinersj = new StringJoiner("/","<",">"); also, StringJoinersj = new StringJoiner("/"); sj.add("Chera"); sj.add("Chola"); sj.add("Pandya"); sj.add("Dhara"); sj.toString() Output <Chera/Chola/Pandya/Dhara>
Virtual Extension Methods public interface DorothyInterface { default String getName(String name) { return name + "Wizard"; } }
JDK 5 Tiger JDK 6 Mustang JDK 7 Dolphin JDK 8 no code name yet…
“That’s not a mistake” - Mark Reinhold JDK 9 • Multi core scalability • Big data support • Hypervisor Integration • Multi – gigabyte heaps • Project Jigsaw • ...