Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction of JAVA8

iwagami
October 14, 2017

Introduction of JAVA8

Introduce New features of JAVA8.

iwagami

October 14, 2017

Transcript

14 slides
  1. JAVA8 • Released on March 18 2014 • Big change

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

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

    to describe anonymous function ◦ No way about anonymous function before Java8 ◦ Use anonymous class instead •
  4. 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
  5. Support full closure • Can access a variable outside lambda

    final Integer c; setCallback((a, b) -> (a+b+c)); // c!!
  6. 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)
  7. 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()) }
  8. 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
  9. 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 ; •
  10. 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
  11. 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!

More Decks by iwagami

Other Decks in Technology