Twitter: @speakjava
§ A new date, time, and calendar API for the Java SE platform
§ Supports standard time concepts
– Partial, duration, period, intervals
– date, time, instant, and time-zone
§ Provides a limited set of calendar systems and be extensible to others
§ Uses relevant standards, including ISO-8601, CLDR, and BCP47
§ Based on an explicit time-scale with a connection to UTC
Compact1 Profile
11Mb
Fork/Join Framework
java.util.concurrent (jsr166y)
(jsr166)
Phasers, etc
java.lang.Thread (jsr166)
1.4 5.0
2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
6 7 8
16 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
The Problem: External Iteration
List<Student> students = ... • Our code controls iteration
double highestScore = 0.0; • Inherently serial: iterate from
for (Student s : students) { beginning to end
if (s.getGradYear() == 2011) { • Not thread-safe because
if (s.getScore() > highestScore) { business logic is stateful
highestScore = s.getScore(); (mutable accumulator
} variable)
}
}
How
20 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Lambda Expression Types
• Single-method interfaces are used extensively in Java
– Definition: a functional interface is an interface with one abstract method
– Functional interfaces are identified structurally
– The type of a lambda expression will be a functional interface
§ Lambda expressions provide implementations of the abstract method
class SessionManager {
long before = ...;
FileFilter x = File::canRead;
interface Collection<E> {
default Stream<E> stream() {
return StreamSupport.stream(spliterator());
}
}
28 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Virtual Extension Methods
Stop right there!
• Err, isn’t this implementing multiple inheritance for Java?
• Yes, but Java already has multiple inheritance of types
• This adds multiple inheritance of behavior too
• But not state, which is where most of the trouble is
• Can still be a source of complexity
• Class implements two interfaces, both of which have default methods
• Same signature
• How does the compiler differentiate?
• Static methods also allowed in interfaces in Java SE 8
maybeGPS.ifPresent(GPSData::printPosition);
thermalReader
.lines()
.mapToDouble(s ->
Double.parseDouble(s.substring(0, s.length() - )))
.map(t -> ((t – 32) / 5 / 9)
.filter(t -> t != currentTemperature)
.peek(t -> listener.ifPresent(l -> l.temperatureChanged(t)))
.forEach(t -> currentTemperature = t);