3. Lambdas and functional
interfaces
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Hello from thread");
}
}).run();
new Thread(() -> System.out.println("Hello from thread")).run()
@FunctionalInterface
public interface Runnable {
public abstract void run();
}
6. Default methods
package java.lang;
public interface Iterable<T> {
void forEach(Consumer<? super T> action);
}
Core interfaces have new methods
This breaks backwards compatibility
package java.lang;
public interface Iterable<T> {
default void forEach(Consumer<? super T> action) {
for (T t : this) { action.accept(t); }
}
}
This doesn’t
7. Static interface methods
interface MyInterface {
void method1();
static void method2() { System.out.println("Hello from static");
}
}
...
MyInterface.method2();
Keep interface related helper methods in the same class rather
than creating a new one
8. Method references
Static reference
class Example {
public static void main(String[] args) {
String[] str = {"one", "two", "3", "four"};
Arrays.sort(str, Example::compareByLength);
Arrays.sort(str, (s1, s2) -> s1.length() - s2.length());
}
public static int compareByLength(String s1, String s2) {
return s1.length() - s2.length();
}
}
9. Method references
Instance reference
@FunctionalInterface // New JDK8 interface
public interface Supplier {
T get();
}
public String function(Supplier<String> supplier) {
return supplier.get();
}
public void example() {
final String x = "A string";
function(() -> x.toString());
function(x::toString);
}
10. Method references
Constructor reference
class Car {}
class Example {
public static Car createCar(Supplier supplier) {
return supplier.get();
}
public static void repair(Car car) {}
public static void main(String[] args) {
Car car = createCar(Car::new);
List cars = Arrays.asList(car);
cars.forEach(Example::repair);
}
}
11. Repeating annotations
class RepeatingAnnotations {
public @interface Filters { // A hidden filter holder
Filter[] value();
}
@Repeatable(Filters.class)
public @interface Filter {
String value();
}
@Filter("filter1")
@Filter("filter2")
public void filterredMethod() {}
}
Repeat yourself
12. Extended annotations
class Annotations {
public @interface NotEmpty {}
public static class Holder<@NonEmpty T> extends @NonEmpty Object{
public void method() throws @NonEmpty Exception {}
}
public static void main(String[] args) {
final Holder<String> holder = new @NonEmpty Holder<String>();
@NonEmpty Collection<@NonEmpty String> strings =
new ArrayList<>();
}
}
Annotate anything
14. Optional
Optional<String> name = Optional.ofNullable(null);
System.out.println("Name is set? " + name.isPresent() );
System.out.println("Name: " + name.orElseGet( () -> "[none]" ));
System.out.println(name.map( s -> "Hey " + s + "!" )
.orElse("Hey Stranger!"));
java.util.Optional
Name is set? false
Name: [none]
Hey Stranger!
15. Streams
for (Student student : students) {
if (student.getName().startsWith("A")){
names.add(student.getName());
}
}
java.util.stream
List<string> names =
students.stream()
.map(Student::getName)
.filter(name -> name.startsWith("A"))
.collect(Collectors.toList());
The old way
The Java8 way
18. Date/Time API
JSR 310 - java.time
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR, cal.get(Calendar.HOUR) + 2);
LocalTime now = LocalTime.now();
LocalTime later = now.plus(2, HOURS);
The old way
The Java8 way
19. Nashorn
JavaScript engine
• Rhino replacement
• Faster (2 to 10x performance boost)
• Full ECMAScript 5.1 support + extensions
• Compiles JS to Java bytecode