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

Whats new in Java 8

Piotr Kafel
February 04, 2015

Whats new in Java 8

Piotr Kafel

February 04, 2015
Tweet

More Decks by Piotr Kafel

Other Decks in Programming

Transcript

  1. Topics • Lambdas • Method reference • Type inference •

    Streams • Default methods • CompletableFuture
  2. Motivation public List<Person> filterPersonsOlderThan18(List<Person> persons) { List<Person> result = new

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

    = new ArrayList<>(); for (Person p : persons) { if (predicate.test(p)) { result.add(p); } } return result; }
  4. 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);
  5. 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()
  6. Translation public class Lambda { Function<String, Integer> f = s

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

    -> Integer.parseInt(s) + offset; static Integer lambda$1(int offset, String s) { return Integer.parseInt(s) + offset; }
  8. 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 !"; }
  9. 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);
  10. Reference types • Static methods • Instance method on particular

    object • Instance method of an arbitrary object of a particular type • Reference to a constructor
  11. Java 6 static <T> T pick(T a1, T a2) {

    return a2; } Serializable s = pick("d", new ArrayList<String>());
  12. “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
  13. Example List<Integer> transactionsIds = transactions.stream() .filter(t -> t.getType() == Transaction.GROCERY)

    .sorted(comparing(Transaction::getValue).reversed()) .map(Transaction::getId) .collect(toList());
  14. Intermediate vs final methods Stateful vs stateless methods Streams cannot

    be reused Laziness IntStream, LongStream, DoubleStream etc.
  15. Example public interface Iterable<T> { // more code here default

    void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } } }
  16. Example CompletableFuture<Merchant> merchantFuture = CompletableFuture.supplyAsync( () -> getMerchantForUser(id)); CompletableFuture.supplyAsync(() ->

    getUser(id)) .thenCombine(merchantFuture, (u, m) -> createTemplateData(u, m)) .thenAccept(templateData -> sendEmail(templateData));