Java 8 will finally deliver us to the promised land of functional programming! So can we stop worrying now about learning Scala and Clojure and the other new kids on the block?
We'll look at what new powers the new features such as Lambdas, Default Methods and Streams give us, as well as what Java is still lacking compared to other functional languages on the JVM.
4. 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
Compact profiles
...
4
torsdag 12. september 13
5. 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
Compact profiles
...
5
torsdag 12. september 13
6. Your Mission
Should you choose to accept it
Sort a list of people
by their names
torsdag 12. september 13
10. Java 7
Collections.sort(people, new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
});
10
torsdag 12. september 13
12. Java 7
Collections.sort(people, new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
});
12
torsdag 12. september 13
13. Java 7
Comparator<Person> byName = new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
};
Collections.sort(people, byName);
13
torsdag 12. september 13
14. Java 7
Comparator<Person> byName = new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
};
sort(people, byName);Collections.
14
torsdag 12. september 13
15. sort(people, byName)
Java 7
Comparator<Person> byName = new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
};
( , ;
Lisp?
15
torsdag 12. september 13
24. C# extension methods
Static methods
Cannot be overridden
One-size fits all solution
24
torsdag 12. september 13
25. Scala Traits
“Interfaces on steroids”
Method implementations
Variables
Everything a class can have – except constructors
Can mix in multiple traits
25
torsdag 12. september 13
27. 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)
27
torsdag 12. september 13
28. 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
28
torsdag 12. september 13
30. SAM
Comparator<Person> byName = new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
};
30
torsdag 12. september 13
31. 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
31
torsdag 12. september 13
32. 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
32
torsdag 12. september 13
33. Lambda
Comparator<Person> byName =
return x.getName().compareTo(y.getName());
};
BodyReturn type?
Parameter list
(Person x, Person y) -> {
33
torsdag 12. september 13
36. Lambda – putting it together
Comparator<Person> byName =
(x, y) -> x.getName().compareTo(y.getName());
people.sort(byName);
36
torsdag 12. september 13
37. Lambda – putting it together
people.sort((x, y) -> x.getName().compareTo(y.getName()));
37
torsdag 12. september 13
38. Lambda vs Java 7
Collections.sort(people, new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
});
38
torsdag 12. september 13
39. Lambda vs Java 7
Collections.sort(people, new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
});
vs
people.sort((x, y) -> x.getName().compareTo(y.getName()));
39
torsdag 12. september 13
42. Method handles
class Person { ...
public int compareByName(Person other) {
return name.compareTo(other.name);
}
}
42
torsdag 12. september 13
43. Method handles
class Person { ...
public int compareByName(Person other) {
return name.compareTo(other.name);
}
}
Comparator<Person> byName =
(Person a, Person b) -> a.compareByName(b);
43
torsdag 12. september 13
44. Method handles
class Person { ...
public int compareByName(Person other) {
return name.compareTo(other.name);
}
}
Comparator<Person> byName =
(a, b) -> a.compareByName(b);
44
torsdag 12. september 13
45. Method handles
class Person { ...
public int compareByName(Person other) {
return name.compareTo(other.name);
}
}
Comparator<Person> byName = Person::compareByName;
45
torsdag 12. september 13
46. Method handles
class Person { ...
public int compareByName(Person other) {
return name.compareTo(other.name);
}
}
Comparator<Person> byName = Person::compareByName;
people.sort(byName);
46
torsdag 12. september 13
47. Method handles
class Person { ...
public int compareByName(Person other) {
return name.compareTo(other.name);
}
}
people.sort(Person::compareByName);
47
torsdag 12. september 13
48. So far, so good?
Extension methods
Lambdas
Method handles
48
torsdag 12. september 13
50. What’s wrong with using List::sort ?
Modifies existing collection
Others may be using it?
Concurrency issues
Performance
50
torsdag 12. september 13
52. The old fashioned way
List<String> namesOfAdults = new ArrayList<>();
for (Person p : people) {
if (p.getAge() >= 18) {
namesOfAdults.add(p.getName());
}
}
52
torsdag 12. september 13
69. Play with it!
Download from http://jdk8.java.net/download.html
Whitepapers at http://openjdk.java.net/projects/lambda/
FAQ http://www.lambdafaq.org/
Supported in IntelliJ IDEA 12 (and NetBeans nightlies?)
69
torsdag 12. september 13
70. Why use X instead?
Java 8 not yet released (duh!)
March 2014?
Will be a long time before you can use it in enterprise dev!
Clojure and Scala available NOW! (JDK 1.5 and 1.6)
Important things are still missing
70
torsdag 12. september 13
73. What’s the big deal?
Functional programming is all about values!
And transformations (functions) computing new values
Concurrency
Parallelism
Robustness
Testability
Better / higher abstractions
Less code, less bugs!
73
torsdag 12. september 13
74. Some help to be found
Immutable collections
Google Guava, FunctionalJava
Concurrency mechanisms
Akka (Actors, STM)
74
torsdag 12. september 13
75. What else is missing?
Type inference
Pattern matching
For comprehensions
Named and default parameters
Properties
Implicit parameters / conversions
and much more...
http://stackoverflow.com/questions/3844745/scala-advantages-after-java-having-closures/3844844#3844844 75
torsdag 12. september 13
76. LambdaJ
Easier manipulation of collections and more, limited closure support
Google Guava
Lots of nice utility classes, some functional programming support
FunctionalJava
Most “pure” FP – but slow, separate collection hierarchy
Funcito
Simplify creation of functions from existing methods
Integrates with Guava, FunctionalJava, Jedi, ...
But I’m stuck in Java 6... Help!
76
torsdag 12. september 13