Slide 1

Slide 1 text

Whats new in Java 8 Piotr Kafel

Slide 2

Slide 2 text

Topics ● Lambdas ● Method reference ● Type inference ● Streams ● Default methods ● CompletableFuture

Slide 3

Slide 3 text

Lambdas

Slide 4

Slide 4 text

Motivation public List filterPersonsOlderThan18(List persons) { List result = new ArrayList<>(); for (Person p : roster) { if (p.getAge() >= 18) { result.add(p); } } return result; }

Slide 5

Slide 5 text

Motivation public List filterPersons(List persons, PersonPredicate predicate) { List result = new ArrayList<>(); for (Person p : persons) { if (predicate.test(p)) { result.add(p); } } return result; }

Slide 6

Slide 6 text

Motivation filterPersons(persons, new PersonPredicate() { public boolean test(Person person) { return p.getAge() >= 18 && p.getAge() <= 25; } }); filterPersons(persons, p -> p.getAge() >= 18 && p.getAge() <= 25);

Slide 7

Slide 7 text

Syntax p -> p.getAge() >= 18 && p.getAge() <= 25 p -> { LOGGER.info("Checking - " + p); return p.getAge() >= 18 && p.getAge() <= 25; } (p1, p2) -> p1.getAge() < p2.getAge()

Slide 8

Slide 8 text

Lambdas vs Anonymous Inner Classes

Slide 9

Slide 9 text

Translation public class Lambda { Function f = s -> Integer.parseInt(s); } static Integer lambda$1(String s) { return Integer.parseInt(s); }

Slide 10

Slide 10 text

Translation int offset = 100; Function f = s -> Integer.parseInt(s) + offset; static Integer lambda$1(int offset, String s) { return Integer.parseInt(s) + offset; }

Slide 11

Slide 11 text

Interesting fact Won't compile ! ( Illegal forward reference ) public class Foo { private final Runnable runnable = () -> System.out.println("I think " + someThought); private final String someThought = "I love burgers !"; }

Slide 12

Slide 12 text

Method References

Slide 13

Slide 13 text

Example Arrays.sort(arrayOfPersons, (Person a, Person b) -> { return a.getBirthday().compareTo(b.getBirthday()); } );

Slide 14

Slide 14 text

Example public class Person { // a lot of code here public static int compareByAge(Person a, Person b) { return a.birthday.compareTo(b.birthday); } } Arrays.sort(arrayOfPersons, Person::compareByAge);

Slide 15

Slide 15 text

Reference types ● Static methods ● Instance method on particular object ● Instance method of an arbitrary object of a particular type ● Reference to a constructor

Slide 16

Slide 16 text

Type Inference

Slide 17

Slide 17 text

Java 6 static T pick(T a1, T a2) { return a2; } Serializable s = pick("d", new ArrayList());

Slide 18

Slide 18 text

Java 7 Map> myMap = new HashMap>(); Map> myMap = new HashMap<>();

Slide 19

Slide 19 text

Java 8 processStringList(Collections.emptyList()); processStringList(Collections.emptyList());

Slide 20

Slide 20 text

Interesting fact Intellij 13 problems with type inference observable.reduce(new ArrayList(), (accu, usersData) -> { accu.addAll(usersData); return accu; });

Slide 21

Slide 21 text

Streams

Slide 22

Slide 22 text

“A sequence of elements from a source that supports aggregate operations”. from http://www.oracle.com/technetwork/articles/java/ma14-java-se-8-streams-2177646.html

Slide 23

Slide 23 text

Example List transactionsIds = transactions.stream() .filter(t -> t.getType() == Transaction.GROCERY) .sorted(comparing(Transaction::getValue).reversed()) .map(Transaction::getId) .collect(toList());

Slide 24

Slide 24 text

Intermediate vs final methods Stateful vs stateless methods Streams cannot be reused Laziness IntStream, LongStream, DoubleStream etc.

Slide 25

Slide 25 text

Default Methods

Slide 26

Slide 26 text

How ? Iterable iterable = new ArrayList<>(listFullOfStrings); iterable.forEach(System.out::println);

Slide 27

Slide 27 text

Example public interface Iterable { // more code here default void forEach(Consumer action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } } }

Slide 28

Slide 28 text

Traits like in Scala, Groovy etc. ?

Slide 29

Slide 29 text

CompletableFuture

Slide 30

Slide 30 text

Example CompletableFuture merchantFuture = CompletableFuture.supplyAsync( () -> getMerchantForUser(id)); CompletableFuture.supplyAsync(() -> getUser(id)) .thenCombine(merchantFuture, (u, m) -> createTemplateData(u, m)) .thenAccept(templateData -> sendEmail(templateData));

Slide 31

Slide 31 text

CompletableFuture vs Observable

Slide 32

Slide 32 text

Thats all folks !