java 8
DESCRIPTION
http://jsug.at/wiki/Meeting_52TRANSCRIPT
![Page 1: Java 8](https://reader030.vdocuments.us/reader030/viewer/2022020306/554f37aab4c905471e8b475f/html5/thumbnails/1.jpg)
.
......
Java 8Build #84
Robert Bachmann
JSUG Meeting #52
1
![Page 2: Java 8](https://reader030.vdocuments.us/reader030/viewer/2022020306/554f37aab4c905471e8b475f/html5/thumbnails/2.jpg)
Outline
• Interface additions• Lambda Syntax• Library additions
I java.util.functionI java.timeI java.util.OptionalI java.util.streams
2
![Page 3: Java 8](https://reader030.vdocuments.us/reader030/viewer/2022020306/554f37aab4c905471e8b475f/html5/thumbnails/3.jpg)
Do try this at home
• JDK8, http://jdk8.java.net/download.html• Optional: IntelliJ IDEA 12.1
3
![Page 4: Java 8](https://reader030.vdocuments.us/reader030/viewer/2022020306/554f37aab4c905471e8b475f/html5/thumbnails/4.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/554f37aab4c905471e8b475f/html5/thumbnails/5.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/554f37aab4c905471e8b475f/html5/thumbnails/6.jpg)
Examples
package java.util.function;
@FunctionalInterfacepublic interface Consumer<T> {void accept(T t);
}
6
![Page 7: Java 8](https://reader030.vdocuments.us/reader030/viewer/2022020306/554f37aab4c905471e8b475f/html5/thumbnails/7.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/554f37aab4c905471e8b475f/html5/thumbnails/8.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/554f37aab4c905471e8b475f/html5/thumbnails/9.jpg)
Lambda syntax
• Used in conjunction with functionalinterfaces
• Alternative to anonymous inner classes
9
![Page 10: Java 8](https://reader030.vdocuments.us/reader030/viewer/2022020306/554f37aab4c905471e8b475f/html5/thumbnails/10.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/554f37aab4c905471e8b475f/html5/thumbnails/11.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/554f37aab4c905471e8b475f/html5/thumbnails/12.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/554f37aab4c905471e8b475f/html5/thumbnails/13.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/554f37aab4c905471e8b475f/html5/thumbnails/14.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/554f37aab4c905471e8b475f/html5/thumbnails/15.jpg)
java.time
• Similar to JodaTime• Instant & Clock• LocalDateTime, LocalDate, LocalTime• OffsetDateTime, OffsetTime• ZonedDateTime
15
![Page 16: Java 8](https://reader030.vdocuments.us/reader030/viewer/2022020306/554f37aab4c905471e8b475f/html5/thumbnails/16.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/554f37aab4c905471e8b475f/html5/thumbnails/17.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/554f37aab4c905471e8b475f/html5/thumbnails/18.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/554f37aab4c905471e8b475f/html5/thumbnails/19.jpg)
Questions?
19