Discovering Lambdas in Java 8

  • View
    151

  • Download
    0

Embed Size (px)

Text of Discovering Lambdas in Java 8

  • Discovering Lambdas in Java8

    by Aleksandra Dmytrenko

  • Java ->

    ?

  • !

  • ?

    ,

    final ( )

    (

    ).

    (

    ?)

    () - ,

    .

  • ?

    ( )

    final const, public (

    ?)

    ( )

    () -

    . .

  • , ?

  • -

  • -> ?

    Imperative style:

    public boolean isPrime(int number) {

    for (int curNumb = 2; curNumb number % curNumb == 0);}

  • public boolean isPrimeImperative(int number) {

    if (number < 0) {

    return false;

    }

    for (int curNumb = 2; curNumb = 0 && IntStream.rangeClosed(2, number.2).noneMatch(curNumb -> number % curNumb == 0);}

  • -

  • ?

    public static int factorialOf(int number) {

    if (number >= 0) {

    int factorial = 1;

    for (int curNumb = 2; curNumb

  • public static int factorialOf(int number) {

    if (number > 0) return number*factorialOf(number-1);

    if (number == 0) return 1;

    else throw new IllegalArgumentException("Factorial can be counted only of not negative numbers.");

    }

    3 !

    .

    ?

    public static int factorialOf(int number) { if (number >= 0) return IntStream.rangeClosed(2, number) .reduce(1, (accResult, curNumb) -> accResult * curNumb); else throw new IllegalArgumentException("Factorial can be counted only of not negative numbers.");}3 !

  • - , .

    .

  • Chuck Norris can do multiple inheritance in

    Java

  • ,

    . !

    ,

    .

  • C , C?

    interface Elephant {

    default String makeSound(String name) {

    return name + ": !";

    }

    }

    interface Dog {

    default String makeSound(String name) {

    return name + ": -";

    }

    }

  • class EveryDogWantsToBeAnElephant implements Dog, Elephant {

    @Override

    public String makeSound(String name) {

    return name + ": ";

    }

    public static void main(final String[] args) {

    EveryDogWantsToBeAnElephant elephantDog = new EveryDogWantsToBeAnElephant();

    Elephant e = new Elephant(){};

    Dog d = new Dog(){};

    System.out.println(e.makeSound(""));

    System.out.println(d.makeSound(""));

    System.out.println(elephantDog.makeSound("-"));

    }}

    C , C?

    : !: --:

  • -

    .

    ?

  • ..

    ,

    interface Dog {

    static String walk() { return " .; }}

    interface Elephant {

    static String walk() { return " ."; }}System.out.println(Elephant.walk());System.out.println(Dog.walk());

    OUTPUT:

    . .

  • (),

    .

    ,

    .

  • : Comparator int compare(T o1, T o2),

    Comparable int compareTo(T o)

    ,

  • 1. -:Elephant e = new Elephant() {

    @Override

    public boolean isProud() {

    return false;

    }

    };

    2. -:Elephant e = () -> { return false; };

    3. :Elephant e = () -> false;

  • interface Elephant {

    boolean isProud();

    }

    interface Dog {

    boolean isProud();

    }

    System.out.println(" : " + e.isProud());

    System.out.println(" : " + d.isProud());

    System.out.println("- : " + elephantDog.isProud());

    OUTPUT:

    : false

    : true

    - : false

  • 1. :public Predicate isGreaterThan2New() { return a -> a > 2;} :public boolean isGreaterThan2Old(Integer a) { return a > 2;}

    2. -:public BiPredicate isGreaterThanFunc() { return (a, b) -> a > b;}

    :public boolean isGreaterThan(Integer a, Integer b) { return a > b;}

    ,

    boolean test(T t);

    boolean test(T t),

    ?

  • 1. :public Function multiplyFuncBy2() { return (a) -> a * 2;} :public Integer multiplyBy2(Integer a) { return a * 2;}

    2. -:public BiFunction multiplyFuncBy() { return (a, b) -> a * b;} :public Integer multiplyFuncBy(Integer a, Integer b) { return a * b;}

    ,

    ,

    ,

  • 1. -, :boolean actual = p.isGreaterThan2New().test(4);boolean actual = p.isGreaterThan2Old(4);p.isGreaterThanNew().test(4, 8);p.isGreaterThanOld(4, 8);

    2. :values.stream().filter(p.isGreaterThan2New( )).collect(Collectors.toList());List actual = values.stream().filter(v -> p.isGreaterThan2(v)).collect(Collectors.toList());

    .

    .

  • ?

    1. -, :int actual = f.multiplyFuncBy2().apply(5);int actual = f.multiplyBy2(5);f.multiplyFuncBy().apply(5, 3);f.multiplyBy(5, 3);

    2. : values.stream().map(f.multiplyFuncBy2()).skip(2).limit(2).collect(Collectors.toList());List actual = values.stream().map(v -> f.multiplyFuncBy().apply(v, 3)).skip(2).limit(2).collect(Collectors.toList());

    apply()

  • Consumer -

    1. void :public void useConsumer(T t){ System.out.print("Consumer says: " + t);}public Consumer useConsumerFunc(){ return (t) -> System.out.print("Consumer says: " + t);}

    2. :consumer.useConsumer("Hello ;)"); consumer.useConsumerFunc().accept("Hello ;)");

    3. : Stream.of(1, 6).forEach(consumer.useConsumerFunc() .andThen(l -> System.out.print("\n")));OUTPUT:

    Consumer says: 1Consumer says: 6

  • Supplier -

    1. 2 - public String useSupplier() { return Thread.currentThread().getStackTrace()[1].getMethodName(); }

    public Supplier useSupplierFunc() { System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName()); return () -> Thread.currentThread().getStackTrace()[1].getMethodName(); }

  • Supplier -

    1. :String actual = supplier.useSupplier();String actual = supplier.useSupplierFunc().get();Stream.of("1 ").forEach(n -> System.out.println(n + supplier.useSupplierFunc()));

    2. :1) useSupplier2) useSupplierFunc lambda$useSupplierFunc$03) useSupplierFunc 1 lambdas.functionalInterface.MySupplier$$Lambda$1/1449621165@3d8c7aca

  • ,

    ,

    Java

    https://github.com/olexandra-dmytrenko/LambdasForConfs.git

    https://github.com/olexandra-dmytrenko/LambdasForConfs.git

  • :)

    Discovering Lambdas in Java8 ! ? ? , ? 8 -> ? - ? 13 14Chuck Norris can do multiple inheritance in Java 16 C , C? C , C? 19 22 ? Consumer - Supplier - Supplier - , , Java :)