Introduction to new features in Java 8 and their use in functional programming, such as lambda methods, extension methods, method handles, the new Stream API, parallellism and laziness.
2. Java 8 – what’s new?
Lambdas, Extension Methods, SAMs, Streams, Method
Handles, Oh My!
Nashorn JS engine
JSR-310: Date & time API
No more PermGen– where classloaders go to die
...
!2
3. Java 8 – what’s new?
Lambdas, Extension Methods, SAMs, Streams, Method
Handles, Oh My!
Nashorn JS engine
JSR-310: Date & time API
No more PermGen– where classloaders go to die
...
!3
5. Java 7
Collections.sort(people, new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
});
!5
7. Java 8
Comparator<Person> byName = new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
};
Single Abstract Method
!
(SAM)
!7
8. Single Abstract Method
Interface with only one (non-default) method
Abstract class with only one abstract method
Sound familiar?
Pretty much every event listener, callback mechanism, ...
Can be replaced by a Lambda
!8
10. SAM
Comparator<Person> byName = new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
};
!10
11. SAM
Comparator<Person> byName = new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
};
BodyReturn type
Parameter list
Method name
Type
!11
12. Lambda
Comparator<Person> byName = new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
};
BodyReturn type
Parameter list
Method name
Type
!12
31. Java 8 extension methods
Extend interfaces with new methods
Compatibility
Default implementation
Override
Requires modification of original interface
!31
32. C# extension methods
“Add” methods to existing types
Without modifying original type
Defined as static methods
Called as instance methods
!32
33. Scala implicit classes
“Add” methods or properties to existing types
Without modifying original type
Implicit wrapper object
!33
34. Java 7 vs 8
Collections.sort(people, new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
});
!
vs
!
people.sort(comparing(Person::getName));
!34
35. So far, so good?
Lambdas
Method handles
Extension methods
!35
39. The old fashioned way
!39
List<RoadData> filtered = new ArrayList<>();
int count = 0;
for (Iterator<RoadData> i = roadData.iterator();
i.hasNext() && count < 10; ) {
RoadData data = i.next();
if (data.getName().contains(nameQuery)) {
filtered.add(data);
count++;
}
}
56. Play with it!
Download – http://www.oracle.com/technetwork/java/
… or https://jdk8.java.net/download.html
Whitepapers – http://openjdk.java.net/projects/lambda/
FAQ – http://www.lambdafaq.org/
Supported in IntelliJ IDEA 12 & 13
Eclipse Java 8 beta plugin and NetBeans 8.0 RC !56
57. Why use X instead?
Java 8 just released
Will be a long time before you can use it in enterprise dev!
Clojure and Scala available NOW! (JDK 1.6)
Important things are still missing
!57
58. Want to know more?
^{Oslo "Socially Functional Programmers" #OsloSFP}
“Haskell på godt og vondt” – tonight 6pm @ Teknologihuset
http://www.meetup.com/Oslo-Socially-Functional/
Functional Programming in Java 8
http://2014.flatmap.no/
!58