Friday, August 26, 2016

JDK 8 Features

New Features

There are dozens of features added to Java 8, the most significant ones are mentioned below −
  • Lambda expression − Adds functional processing capability to Java.
  • Method references − Referencing functions by their names instead of invoking them directly. Using functions as parameter.
  • Default method − Interface to have default method implementation.
  • New tools − New compiler tools and utilities are added like ‘jdeps’ to figure out dependencies.
  • Stream API − New stream API to facilitate pipeline processing.
  • Date Time API − Improved date time API.
  • Optional − Emphasis on best practices to handle null values properly.
  • Nashorn, JavaScript Engine − A Java-based engine to execute JavaScript code.
 1) Lambda expression :

A lambda expression provides a way to represent one method interface using an expression. A lambda expression is like a method, it provides a list of formal parameters and a body (which can be an expression or a block of code) expressed in terms of those parameters.


The basic syntax of a lambda is either:
(parameters) ->expression                or         (parameters) ->{ statements; }
We can read final local variables from the outer scope of lambda expressions.
In constrast to local variables we have both read and write access to instance fields and static variables from within lambda expressions. Default methods cannot be accessed from within lambda expressions.


Predicates

Predicates are boolean-valued functions of one argument. The interface contains various default methods for composing predicates to complex logical terms (and, or, negate)
Predicate<String> predicate = (s) -> s.length() > 0;

predicate.test("foo");              // true
predicate.negate().test("foo");     // false

Predicate<Boolean> nonNull = Objects::nonNull;
Predicate<Boolean> isNull = Objects::isNull;

Predicate<String> isEmpty = String::isEmpty;
Predicate<String> isNotEmpty = isEmpty.negate();

Functions

Functions accept one argument and produce a result. Default methods can be used to chain multiple functions together (compose, andThen).
Function<String, Integer> toInteger = Integer::valueOf;
Function<String, String> backToString = toInteger.andThen(String::valueOf);

backToString.apply("123");     // "123"

Suppliers

Suppliers produce a result of a given generic type. Unlike Functions, Suppliers don't accept arguments.
Supplier<Person> personSupplier = Person::new;
personSupplier.get();   // new Person

Consumers

Consumers represents operations to be performed on a single input argument.
Consumer<Person> greeter = (p) -> System.out.println("Hello, " + p.firstName);
greeter.accept(new Person("Luke", "Skywalker"));

Comparators

Comparators are well known from older versions of Java. Java 8 adds various default methods to the interface.
Comparator<Person> comparator = (p1, p2) -> p1.firstName.compareTo(p2.firstName);

Person p1 = new Person("John", "Doe");
Person p2 = new Person("Alice", "Wonderland");

comparator.compare(p1, p2);             // > 0
comparator.reversed().compare(p1, p2);  // < 0



Working with Lambdas and Streams

            Streams are wrappers around collections that use lambdas pervasively. They support many operations that use lambdas, like map, filter, limit, sorted, count, min, max, sum, collect and others. 


Reference article :- 
http://cr.openjdk.java.net/~briangoetz/lambda/lambda-translation.html
http://www.developer.com/java/start-using-java-lambda-expressions.html
http://www.infoq.com/articles/Java-8-Lambdas-A-Peek-Under-the-Hood



2] Method References :- Java 8 enables you to pass references of methods or constructors via the :: keyword.



3] Default method :-  Java 8 enables us to add non-abstract method implementations to interfaces by utilizing the default keyword. This feature is also known as Extension Methods. In Java 8 in interface default , static methods are allowed. Instance variables are not allowed as fields declared in interface are static final  i.e. constant.

4] Functional Interface :- How does lambda expressions fit into Javas type system? Each lambda corresponds to a given type, specified by an interface. A so called functional interface must contain exactly one abstract method declaration. Each lambda expression of that type will be matched to this abstract method. Since default methods are not abstract you're free to add default methods to your functional interface.