Slide 1

Slide 1 text

Welcome to JAVA8 Gammy(iwag) Oct. 14 2017 Froghouse LT

Slide 2

Slide 2 text

JAVA8 ● Released on March 18 2014 ● Big change of JAVA ○ Lambda, stream, ... ●

Slide 3

Slide 3 text

JAVA9 ● Released on September 21 2017 !!! ● Module systems, etc… ● Not mentioned today...

Slide 4

Slide 4 text

NEW1 Lambda expression; (var1, var2) -> () ● A way to describe anonymous function ○ No way about anonymous function before Java8 ○ Use anonymous class instead ●

Slide 5

Slide 5 text

Example JAVA7 sort(..., new Comparator{ int compare(Type a, Type b) { Return a.getValue() - b.getValue(); } }); JAVA8 sort(..., (Type a, Type b) -> { return a.getValue() - b.getValue(); }); OR sort(..., (a, b) -> (a.getValue() - b.getValue)) // !!! Smart

Slide 6

Slide 6 text

Support full closure ● Can access a variable outside lambda final Integer c; setCallback((a, b) -> (a+b+c)); // c!!

Slide 7

Slide 7 text

FunctionalInterface ● What type/class is anonymous function? ○ Necessary; Java is static type language ○ => FunctionalInterface ● Many buildin interfaces in java.util.function Function f = (Type a) -> ( (TypeB)f(a)); // TypeA to TypeB BiFunction g=(TypeA a, TypeB b) -> ((TypeC)f(a,b)

Slide 8

Slide 8 text

NEW2: Stream API ● Allow to use Method Chain for collection ○ Like Ruby, JS but... EX: Int charcount( List slist) { Return slist.stream().map(s -> s.length).filter(i -> i.length>10).count(); } List StringListToIntList(List list) { Return list.stream().map(i=>String.valueOf(i)).collector(Collectors.toList()) }

Slide 9

Slide 9 text

How to use Stream: 3 Step ● Special steps to use stream ○ Because stream is newly introduced concept in Java ● 1 Create a stream; ○ Call collection.stream() ● 2 Operation ○ Map, filter, flatMap, count, find … see doc ● 3 Terminal Operation ○ Call Collector … see doc

Slide 10

Slide 10 text

NEW3 Optional - Free from NullPointerException ● Optional is a type which represents may or may not have a value ○ Before optional, use null in usual ● Check calling isPresent() ○ True: exists a valuve ○ False: Doesn’t exists ; ●

Slide 11

Slide 11 text

Example ● Optional optInt; ● Two cases ○ Has a value: optInt.get() = 0, 1, ,2 ….. . optInt. isPresent() = true ○ Doesn’t have value: optInt.get() = exception optInt.isPresent() = false

Slide 12

Slide 12 text

Example: String List with null value to Integer List

Slide 13

Slide 13 text

Example: String List with null value to Integer List stringListWithNull.stream() .map(s->Optional.of(s)) // List> .map(s->s.map(u->Integer.valueOf(s)).orElse(0)) // List .collect(Collectors.toList()) Some(1) / None // if no optional, we must insert if-null in all time!

Slide 14

Slide 14 text

NEW4: Future; True future To be continued