What's new in Java 8

38
What’s new in Java 8 Kyle Smith linkedin.com/in/kylesm @kylesm

Transcript of What's new in Java 8

Page 1: What's new in Java 8

What’s new in Java 8Kyle Smith

linkedin.com/in/kylesm

@kylesm

Page 2: What's new in Java 8

• Concurrency improvements

• Default methods

• Functional interfaces

• Lambda expressions

• Method references

• Streams

• Date/time

• Optional

• JavaScript

• JVM PermGen

• Base64 support

• Compact profiles

• Annotation improvements

• Type inference improvements

• Method parameter reflection

• JDBC 4.2

Page 3: What's new in Java 8

• Concurrency improvements

• Default methods

• Functional interfaces

• Lambda expressions

• Method references

• Streams

• Date/time

• Optional

• JavaScript

• JVM PermGen

• Base64 support

• Compact profiles

• Annotation improvements

• Type inference improvements

• Method parameter reflection

• JDBC 4.2

Page 4: What's new in Java 8

Themes

Page 5: What's new in Java 8
Page 6: What's new in Java 8
Page 7: What's new in Java 8
Page 8: What's new in Java 8
Page 9: What's new in Java 8

• ConcurrentHashMap — all new (inside)!

• LongAdder, DoubleAdder

• StampedLock

• CompletableFuture

Page 10: What's new in Java 8

Adders

Page 11: What's new in Java 8

• New classes for concurrent counters

• Waaaayyy more scalable than AtomicLong,etc.

• …if you can tolerate some inaccuracy

Page 12: What's new in Java 8

LongAdder clicks = new LongAdder();

clicks.increment();

// or counter.add(long)

// ...

clicks.sum();

Page 13: What's new in Java 8
Page 14: What's new in Java 8

StampedLock

Page 15: What's new in Java 8

• Alternative to ReentrantReadWriteLock

• “does not consistently prefer readers over writers or

vice versa"

• Provides optimistic reads

Page 16: What's new in Java 8

void move(double deltaX, double deltaY){

long stamp = sl.writeLock();

try {

x += deltaX;

y += deltaY;

} finally {

sl.unlockWrite(stamp);

}

}

Page 17: What's new in Java 8

double distanceFromOrigin() {

long stamp = sl.tryOptimisticRead();

double currentX = x, currentY = y;

if (!sl.validate(stamp)) {

stamp = sl.readLock();

try {

currentX = x;

currentY = y;

} finally {

sl.unlockRead(stamp);

}

}

return Math.sqrt(currentX * currentX

+ currentY * currentY);

}

Page 18: What's new in Java 8

CompletableFuture

Page 19: What's new in Java 8

• Everybody wants to be event-driven & reactive

• CompletableFuture allows composition of async

work

Page 20: What's new in Java 8

CompletableFuture<String> f1 = //…

CompletableFuture<Double> f2 = f1

.thenApply(Integer::parseInt)

.thenApply(r -> r * r * Math.PI);

Page 21: What's new in Java 8

Default methods

Page 22: What's new in Java 8

• Interfaces can supply non-abstract methods

• Enables existing interfaces to evolve

• Eliminates the need for companion (“garbage”)

classes

Page 23: What's new in Java 8

// java.lang.Iterable

default void forEach(Consumer<? super T>

action) {

Objects.requireNonNull(action);

for (T t : this) {

action.accept(t);

}

}

Page 24: What's new in Java 8

Functional interfaces

Page 25: What's new in Java 8

• Any interface with a single abstract method

• Examples:

• java.util.Comparator

• java.lang.Runnable

• Can be annotated with @FunctionalInterface

Page 26: What's new in Java 8

Lambda expressions

Page 27: What's new in Java 8

• Function values in Java

• Compatible with a functional interface when their

shapes match

• Syntax: (parameters) -> expression

Page 28: What's new in Java 8

DoubleUnaryOperator op1 =

(double r) -> r * r * Math.PI;

DoubleUnaryOperator op2 =

(r) -> r * r * Math.PI;

Runnable r =

() -> System.out.println("Hi, NEJUG!");

void cut(List<String> l, int len) {

l.replaceAll(s -> s.substring(0, len));

}

Page 29: What's new in Java 8

Streams

Page 30: What's new in Java 8

public Collection<Person>

findStudents(List<Person> people) {

Collection<Person> students =

new ArrayList<>();

for (Person p : people) {

if (p.isStudent()) {

students.add(p);

}

}

return students;

}

Page 31: What's new in Java 8

public Collection<Person>

findStudents(List<Person> people) {

Collection<Person> students =

new ArrayList<Person>();

people.forEach(i -> {

if (i.isStudent()) {

students.add(i);

}

});

return students;

}

Page 32: What's new in Java 8

public Collection<Person>

findStudents(List<Person> people) {

return people

.stream()

.filter(p -> p.isStudent())

.collect(Collectors.toList());

}

Page 33: What's new in Java 8

• “ A sequence of elements on which one or more

operations can be performed”

• Operations are either intermediate or terminal

• Streams can be sequential or parallel

Page 34: What's new in Java 8

• Intermediates:

• map, filter, flatMap, sorted, allMatch, anyMatch,

findAny, findFirst, flatMap, …

• Terminal:

• collect, count, forEach,reduce, max, min, …

Page 35: What's new in Java 8

long count = values

.stream()

.sorted()

.count();

long count = values

.parallelStream()

.sorted()

.count();

Page 36: What's new in Java 8

What next?

• Download JDK 8 from java.oracle.com

• IDE Support:

• Eclipse 4.3.2 SR2 (with patches!)

• IntelliJ IDEA 12.0+

• NetBeans IDE 8.0

Page 37: What's new in Java 8

Can’t move to Java 8?

• Google Guava (Java 6+)

• jsr166e (Java 6+)

• CHMv8, F/J tweaks, StampedLock, etc.

• Mix in some Groovy

• GPars

Page 38: What's new in Java 8

References:

http://bit.ly/1fRGyQV