java 8

20
. . Java 8 Build #84 Robert Bachmann JSUG Meeting #52 1

Upload: robert-bachmann

Post on 10-May-2015

827 views

Category:

Technology


0 download

DESCRIPTION

http://jsug.at/wiki/Meeting_52

TRANSCRIPT

Page 1: Java 8

.

......

Java 8Build #84

Robert Bachmann

JSUG Meeting #52

1

Page 2: Java 8

Outline

• Interface additions• Lambda Syntax• Library additions

I java.util.functionI java.timeI java.util.OptionalI java.util.streams

2

Page 3: Java 8

Do try this at home

• JDK8, http://jdk8.java.net/download.html• Optional: IntelliJ IDEA 12.1

3

Page 4: Java 8

Interface additions

• Can add static methods to interfaces• Can add default methods to interfaces• Functional interfaces: An interface with oneabstract method

4

Page 5: Java 8

Examples

package java.util;

public interface List<E>extends Collection<E> {

/* [...] */

default void sort(Comparator<? super E> c) {Collections.sort(this, c);

}}

5

Page 6: Java 8

Examples

package java.util.function;

@FunctionalInterfacepublic interface Consumer<T> {void accept(T t);

}

6

Page 7: Java 8

Examples

public interface Iterable<T> {Iterator<T> iterator();

default void forEach(Consumer<? super T> action) {

Objects.requireNonNull(action);for (T t : this) {

action.accept(t);}

}}

7

Page 8: Java 8

Default method rules

• Speci c interface over parent interface• Object method over default method• Can not provide toString, hashCode, equals

8

Page 9: Java 8

Lambda syntax

• Used in conjunction with functionalinterfaces

• Alternative to anonymous inner classes

9

Page 10: Java 8

Examples

List<String> list = Arrays.asList(”a”,”b”);

list.forEach(new Consumer<String>() { // Java 7public void accept(String s){ System.out.println(s); }

});

// Java 8list.forEach( s -> {System.out.println(s);} );

list.forEach( s -> System.out.println(s) );

list.forEach( System.out::println );

10

Page 11: Java 8

Examples

List<Person> personList = ...;

// Java <= 7Collections.sort(personList,new Comparator<Person>() {@Overridepublic int compare(Person p1, Person p2) {return Long.compare(p1.getAge(), p2.getAge());

}});

11

Page 12: Java 8

Examples

List<Person> personList = ...;

// Java 8personList.sort((p1,p2) -> {return Long.compare(p1.getAge(), p2.getAge())

});

// or:personList.sort((p1, p2) -> Long.compare(p1.getAge(), p2.getAge()));

// alternative approach:personList.sort(Comparators.comparing(Person::getAge));

12

Page 13: Java 8

java.util.function (1/2)

• Consumer (T→ void)• Supplier (void→ T)• Predicate (T→ boolean)• Function (T→ R) and UnaryOperator (T→ T)• BiFunction, BiPredicate, BinaryOperator

13

Page 14: Java 8

java.util.function (2/2)

• IntConsumer (int→ void)• IntSupplier (void→ int)• IntPredicate (int→ boolean)• IntFunction (int→ R)• IntUnaryOperator (int→ int)• ToIntFunction (T→ int)• ObjIntConsumer, ToIntBiFunction• Same for Double and Long

14

Page 15: Java 8

java.time

• Similar to JodaTime• Instant & Clock• LocalDateTime, LocalDate, LocalTime• OffsetDateTime, OffsetTime• ZonedDateTime

15

Page 16: Java 8

java.util.Optional

Optional<String> name = f();

// example 1System.out.println(”Hello␣” + name.orElse(”user”));

// example 2name.ifPresent( s -> System.out.println(”Hello␣” + s) );

16

Page 17: Java 8

java.util.stream

// JDK exampleint sumOfWeights =blocks.stream()

.filter(b -> b.getColor() == RED)

.mapToInt(b -> b.getWeight())

.sum();

// Example with personListpersonList // List<Person>.stream() // Stream<Person>.filter(p -> p.getAge() >= 18) // Stream<Person>.map( p -> p.getName() ) // Stream<String>.forEach(System.out::println) // void;

17

Page 18: Java 8

Further reading

• http://www.techempower.com/blog/2013/03/26/everything-about-java-8/

• http://javadocs.techempower.com/jdk18/api/overview-summary.html

• src.zip in your JDK 8 installation

18

Page 19: Java 8

Questions?

19

Page 20: Java 8

Thanks

Twitter @robertbachmann

Email rb@ — .at

20