What's new in Java 8

Post on 15-Jul-2015

131 views 2 download

Transcript of What's new in Java 8

What’s new in Java 8Kyle Smith

linkedin.com/in/kylesm

@kylesm

• 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

• 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

Themes

• ConcurrentHashMap — all new (inside)!

• LongAdder, DoubleAdder

• StampedLock

• CompletableFuture

Adders

• New classes for concurrent counters

• Waaaayyy more scalable than AtomicLong,etc.

• …if you can tolerate some inaccuracy

LongAdder clicks = new LongAdder();

clicks.increment();

// or counter.add(long)

// ...

clicks.sum();

StampedLock

• Alternative to ReentrantReadWriteLock

• “does not consistently prefer readers over writers or

vice versa"

• Provides optimistic reads

void move(double deltaX, double deltaY){

long stamp = sl.writeLock();

try {

x += deltaX;

y += deltaY;

} finally {

sl.unlockWrite(stamp);

}

}

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);

}

CompletableFuture

• Everybody wants to be event-driven & reactive

• CompletableFuture allows composition of async

work

CompletableFuture<String> f1 = //…

CompletableFuture<Double> f2 = f1

.thenApply(Integer::parseInt)

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

Default methods

• Interfaces can supply non-abstract methods

• Enables existing interfaces to evolve

• Eliminates the need for companion (“garbage”)

classes

// java.lang.Iterable

default void forEach(Consumer<? super T>

action) {

Objects.requireNonNull(action);

for (T t : this) {

action.accept(t);

}

}

Functional interfaces

• Any interface with a single abstract method

• Examples:

• java.util.Comparator

• java.lang.Runnable

• Can be annotated with @FunctionalInterface

Lambda expressions

• Function values in Java

• Compatible with a functional interface when their

shapes match

• Syntax: (parameters) -> expression

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));

}

Streams

public Collection<Person>

findStudents(List<Person> people) {

Collection<Person> students =

new ArrayList<>();

for (Person p : people) {

if (p.isStudent()) {

students.add(p);

}

}

return students;

}

public Collection<Person>

findStudents(List<Person> people) {

Collection<Person> students =

new ArrayList<Person>();

people.forEach(i -> {

if (i.isStudent()) {

students.add(i);

}

});

return students;

}

public Collection<Person>

findStudents(List<Person> people) {

return people

.stream()

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

.collect(Collectors.toList());

}

• “ 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

• Intermediates:

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

findAny, findFirst, flatMap, …

• Terminal:

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

long count = values

.stream()

.sorted()

.count();

long count = values

.parallelStream()

.sorted()

.count();

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

Can’t move to Java 8?

• Google Guava (Java 6+)

• jsr166e (Java 6+)

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

• Mix in some Groovy

• GPars

References:

http://bit.ly/1fRGyQV