what's new in java 8

25
What’s New in Java 8 Lambda, Default Methods, and Streams

Upload: dian-aditya

Post on 16-Apr-2017

38 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: What's new in java 8

What’s New in Java 8Lambda, Default Methods, and Streams

Page 2: What's new in java 8

What’s New in Java 8

● Lambda Expression● Default Methods● Streams● Optional● Nashorn● New Date and Time API● No More PermGen

Page 3: What's new in java 8

What’s New in Java 8

● Lambda Expression● Default Methods● Streams● Optional● Nashorn● New Date and Time API● No More PermGen

Page 4: What's new in java 8

Java 9 has been scheduled for general availability in 2017

Page 5: What's new in java 8

Lambda Expression and Method Reference

Page 6: What's new in java 8

Lambda Expression

A lambda expression is like syntactic sugar for an anonymous class with one method

Page 7: What's new in java 8

Lambda Expression - Syntax

() -> body

parameter -> body

(parameters) -> body

Page 8: What's new in java 8

Lambda Expression - Syntax Example

() -> { System.out.println(“Hello”); }

(int i) -> System.out.println(i)

(double d1, double d2) -> Double.compare(d1, d2)

Page 9: What's new in java 8

Lambda Expression - Method References

A method references is the shorthand syntax for a lambda expression that executes just one

method

Page 10: What's new in java 8

Lambda Expression - Method References

Sometimes lambda expression is just a call to some method

list.forEach(s -> System.out.println(s));

Page 11: What's new in java 8

Lambda Expression - Method References

You can turn that lambda expression into method reference

list.forEach(System.out::println);

Page 12: What's new in java 8

Lambda Expression - Method References

Consumer<String> consumer = (s) -> { System.out.println(s)};

Consumer<String> consumer = System.out::println;

Page 13: What's new in java 8

Default Methods

Page 14: What's new in java 8

Default Methods

Default methods enable you to add new functionality to the interfaces of your libraries

and ensure binary compatibility with code written for older versions of those interfaces.

Page 15: What's new in java 8

Default Methods - Example

public interface Iterable<T> { Iterator<T> iterator(); default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } } default Spliterator<T> spliterator() { ... }}

Page 16: What's new in java 8

Static MethodsYou can define static methods in interfaces!

Page 17: What's new in java 8

Static Methods - Example

public interface Vertx extends Measured { static Vertx vertx() { return factory.vertx(); } ... VertxFactory factory = ServiceHelper.loadFactory(VertxFactory.class);}

Page 18: What's new in java 8

Static Methods - Example

Vertx vertx = Vertx.vertx();

Page 19: What's new in java 8

Static Methods - Example

Vertx vertx = Vertx.vertx();

Page 20: What's new in java 8

Streams

Page 21: What's new in java 8

Streams

A sequence of elements supporting sequential and parallel aggregate operations.

Page 22: What's new in java 8

Streams - Example

int sum = widgets.stream() .filter(w -> w.getColor() == RED) .mapToInt(w -> w.getWeight()) .sum();

Page 23: What's new in java 8

Streams - Example

int sum = widgets.parallelStream() .filter(w -> w.getColor() == RED) .mapToInt(w -> w.getWeight()) .sum();

Page 24: What's new in java 8

?

Page 25: What's new in java 8

Thank You