Cool in Java 8, and New in Java 9 PDF
Cool in Java 8, and New in Java 9 PDF
Aurelio Garcia-Ribeyro
Director of Product Management
Java Platform Group
March 2017
Lambda Expressions
Default Methods
Method References
Date Time APIs - JSR 310
Iterator<Person> it = people.iterator();
while (it.hasNext()) {
Person p = it.next();
if (p.getAge() > 18)
it.remove();
}
class Collections {
public static<T>
void removeIf(Collection<T> coll,
Predicate<T> pred) {
...
}
}
Collections.removeIf(people,
new Predicate<Person>() {
public boolean test(Person p) {
return p.getAge() > 18;
}
}
});
Collections.removeIf(people,
new Predicate<Person>() {
public boolean test(Person p) {
return p.getAge() > 18;
}
}
});
Collections.removeIf(people,
p -> p.getAge() > 18);
int highestWeight = 0;
for (Person p : people) {
if (p.getGender() == MALE) {
int weight = p.getWeight();
highestWeight = max(highestWeight,weight);
}
}
int highestWeight =
people.stream()
.filter(p -> p.getGender() == MALE)
.mapToInt(p -> p.getWeight())
.max();
}
Copyright 2017, Oracle and/or its affiliates. All rights reserved.
Parallelism
Collection<Person> people = ...;
int highestWeight =
people.stream()
people.parallelStream()
.filter(p -> p.getGender() == MALE)
.mapToInt(p -> p.getWeight())
.max();
Lambda Expressions
Default Methods
Method References
Date Time APIs - JSR 310
int highestWeight =
people.stream()
...
interface Collection<T> {
...
default Stream<T> stream() {
...
}
}
Lambdas make code read more like the problem statement. The resulting
code is clear, concise, and easy to maintain and modify.
Quickly convert
anonymous
inner
classes
to lambdas
Easily convert
from lambdas
to method references
Specify a scope
for upgrading to Java 8
All/current projects
Specific package
Specific class
Run converters
Visually preview
proposals for
refactoring
Lambda Expressions
Default Methods
Method References
Date Time APIs - JSR 310
time = time.plusHours(4).minusMinutes(1).withNano(0);
time = time.truncatedTo(ChronoUnit.SECONDS);
dt1 = LocalDateTime.now();
dt2 = LocalDateTime.of(2013,SEPTEMBER,12, 13,30);
dt1 = dt1.plusDays(2).minusHours(1);
dt1 = dt1.with(next(TUESDAY));
dt2.toString(); // 2013-09-12T13:30
dt2 = dt2.truncatedTo(MINUTES);
Copyright 2017, Oracle and/or its affiliates. All rights reserved. http://www.flickr.com/photos/estevesm/473866656/
Instant
l Stores nanoseconds from 1970-01-01Z
l Closest equivalent to java.util.Date
l Use case: timestamp in logging
instant1 = Instant.now();
instant2 = Instant.now();
if (instant1.isAfter(instant2)) { }
dt = LocalDateTime.now();
dt = dt.plus(duration);
dt = LocalDateTime.now();
dt = dt.plus(period);
https://www.oracle.com/java8launch
Copyright 2017, Oracle and/or its affiliates. All rights reserved.
Coming in JDK 9
Create a tool that can assemble and optimize a set of modules and their
dependencies into a custom run-time image as defined in JEP 220. Define a
plugin mechanism for transformation and optimization during the assembly
process, and for the generation of alternative image formats
Create a custom runtime optimized for a single program
JEP 261 defines link time as an optional phase between the phases of
compile time and run time. Link time requires a linking tool that will
assemble and optimize a set of modules and their transitive dependencies
to create a run-time image or executable
@Deprecated(since=9, condemned=true)
Revamp the deprecation annotation, and provide tools to strengthen the
API life cycle
Provide better information about the status and intended disposition of
APIs in the specification
Provide a tool to analyze an application's static usage of deprecated APIs
Provide a tool to detect an application's dynamic usage of of deprecated
APIs in order to emit warnings at runtime
Decrease the amount of code needed for creating small collections and
maps
Implement OCSP stapling via the TLS Certificate Status Request extension
and the Multiple Certificate Status Request Extension
Certificate status checking using OCSP typically involves a network request
for each certificate being checked. Because of the additional network
requests, enabling OCSP checking for TLS on the client side can have a
significant impact on performance.
OCSP stapling allows the presenter of a certificate, rather than the issuing
Certificate Authority (CA), to bear the resource cost of providing OCSP
responses
Make most of the JDK's internal APIs inaccessible by default but leave a few
critical, widely-used internal APIs accessible, until supported replacements
exist for all or most of their functionality
In order to keep critical APIs without a replacement accessible by default sun.misc
and sun.reflect will not be hidden and a few APIs kept public
sun.misc.Unsafe
sun.misc.{Signal,SignalHandler}
sun.reflect.Reflection::getCallerClass
sun.reflect.ReflectionFactory
All other APIs in these packages (e.g. sun.misc.Base64) will be removed
Integrate features from Project Jigsaw into the Java Packager, including
module awareness and custom runtime creation
The packager will only create applications that use the JDK 9 runtime.
Compile Java classes to native code prior to launching the virtual machine.
Improve the start-up time of both small and large Java applications, with at
most a limited impact on peak performance.
AOT compilation is done by a new tool, jaotc
For the initial release, the only supported module is java.base.
AOT compilation of any other JDK module, or of user code, is experimental
Address the rough edges of the changes included in Project Coin / JSR 334
as part of JDK 7 / Java SE 7
https://wiki.openjdk.java.net/display/JDK8/Java+Dependency+Analysis+Tool