Introduction of JAVA8

7a1631691f372ed25308ba9ccd727b2d?s=47 iwagami
October 14, 2017

Introduction of JAVA8

Introduce New features of JAVA8.

7a1631691f372ed25308ba9ccd727b2d?s=128

iwagami

October 14, 2017
Tweet

Transcript

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

  2. JAVA8 • Released on March 18 2014 • Big change

    of JAVA ◦ Lambda, stream, ... •
  3. JAVA9 • Released on September 21 2017 !!! • Module

    systems, etc… • Not mentioned today...
  4. NEW1 Lambda expression; (var1, var2) -> () • A way

    to describe anonymous function ◦ No way about anonymous function before Java8 ◦ Use anonymous class instead •
  5. Example JAVA7 sort(..., new Comparator<Type>{ 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
  6. Support full closure • Can access a variable outside lambda

    final Integer c; setCallback((a, b) -> (a+b+c)); // c!!
  7. FunctionalInterface • What type/class is anonymous function? ◦ Necessary; Java

    is static type language ◦ => FunctionalInterface • Many buildin interfaces in java.util.function Function<TypeA, TypeB> f = (Type a) -> ( (TypeB)f(a)); // TypeA to TypeB BiFunction<TypeA, TypeB, TypeC> g=(TypeA a, TypeB b) -> ((TypeC)f(a,b)
  8. NEW2: Stream API • Allow to use Method Chain for

    collection ◦ Like Ruby, JS but... EX: Int charcount( List<String> slist) { Return slist.stream().map(s -> s.length).filter(i -> i.length>10).count(); } List<Integer> StringListToIntList(List<String> list) { Return list.stream().map(i=>String.valueOf(i)).collector(Collectors.toList()) }
  9. 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
  10. 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 ; •
  11. Example • Optional<Integer> optInt; • Two cases ◦ Has a

    value: optInt.get() = 0, 1, ,2 ….. . optInt. isPresent() = true ◦ Doesn’t have value: optInt.get() = exception optInt.isPresent() = false
  12. Example: String List with null value to Integer List

  13. Example: String List with null value to Integer List stringListWithNull.stream()

    .map(s->Optional.of(s)) // List<Optional<String>> .map(s->s.map(u->Integer.valueOf(s)).orElse(0)) // List<String> .collect(Collectors.toList()) Some(1) / None // if no optional, we must insert if-null in all time!
  14. NEW4: Future; True future To be continued