Pages

Wednesday 18 June 2014

Some features of Java 8.

Some Java 8 features.

Although the java has launched its new version Java 8 with various new features, some of them are as. Because of this release there will be a drastic change in the way of coding using Lambda expressions, Streams, default interfaces, etc.

  • Streams.
  • Functional Interfaces.
  • Lambda.
  • Java Time API.
  • Accumulators.

Streams


Streams are not the streams you are thinking about (InputStream, OutputStream) this stream is all about collections, well this stream is not for replacing the List, ArrayList or any collection. There are generally two types of stream in the collections.
  1. Sequential Stream.
  2. Parallel Stream.
Sequential Stream: - When the stream is traversed sequentially it traverses one by one.

Example:-

List<Address> addresses = list.getStream.collect(Collectors.toList());



Parallel Stream: - When the stream is traversed parallely it breaks into many parts and then gets traversed and each of which is processed individually on a different thread. The results are consolidated output is formed.

Example:-

List<Address> addresses = list.getStream.parallel().collect(Collectors.toList());

Functional Interfaces

The interface which has default method, which can give a default behaviour, which can give a default functionality, this type of interfaces are the functional interface.

Functional interface cannot have more than one abstract method but it can have more than one default methods. Default methods are introduced in Java 8, to add new methods to interface without disturbing the implemented classes.

Example:-

interface FunctionalInterface{
  void show();
  default void display(){
     System.out.println("Default method in functional interface can have body..!");
  }

}

Lambda

Lambda expression is nothing but a method without any declaration, access modifiers, return value and name. It’s a kind of expression which is unspecified function. By the use of this we can directly write the logic without extracting as a method that saves our effort.

Lambda expression looks like:-

(Argument) -> {Body}
(Type argument1, Type argument2...) -> {Body}

Examples:-

(Integer x, Integer y) -> {return x * Y;}

(String s) -> {System.out.println(s);}


Java Time API


Well Java date and Time API is very old and it is continuously getting developed. Finally replacing the confusing, slow, and difficult Date and Calendar APIs. Although all the java Time classes are immutable and thread safe. They are based on the ISO 8601 calendar system, the de facto world calendar following the proleptic Gregorian rules

This time with use of Java Time API packages it will be quite easy. Though it contains some sub-packages java.time.format that provides classes to print and parse dates and times and java.time.zone provides support for time-zones and their rules.
The new Time API prefers enums over integer constants for months and days of the week. One of the useful class is DateTimeFormatter for converting datetime objects to strings

Accumulators

An accumulator is a variable that the program uses to calculate a sum or product of a series of values. Accumulator variable is used to maintain a single count, sum... which is updated by many threads. Therefore some new classes were generated for it.

·         DoubleAccumulator
·         DoubleAdder
·         LongAccumulator
·         LongAdder

Generally these classes are used when the variable is to accessed by many threads.
Both the DoubleAdder and LongAdder classes can be seen as specific subsets of the DoubleAccumulator and LongAccumulator functionality.

The call new DoubleAdder() is equivalent to
new DoubleAccumulator((x, y) -> x + y, 0.0).
The call new LongAdder() is equivalent to
new LongAccumulator((x, y) -> x + y, 0L).

Example:-

DoubleAccumulator da = new DoubleAccumulator((x,y) -> x + y, 0.0);
List<Double> doubles = Arrays.asList(1.0, 2.0, 3.0, 4.0, 10.0);
doubles.forEach(da::accumulate);

System.out.println("Result: " + da.doubleValue());

Result:-
Result: 24

LongAdder la = new LongAdder();
List<long> longs = Arrays.asList(10, 20, 30, 40, 100);
longs.forEach(la::accumulate);

System.out.println("Result: " + la.longValue());

Result:-

Result: 200