Java 8 New Features :
a)Optional Class
b)Lambdas Expression
c) Parallel Arrays With Examples
b)Lambdas Expression
c) Parallel Arrays With Examples
Java 8 is already released by Oracle last year
It is the most feature rich update than
previous 6 and 7 which both have some
minor updates.
The Major features are as fallows:
- Introduction of Optional class
- Parallel Arrays
- Lambdas Expression
Introduction of Optional:
The main benfit of Optional is to avoid NullPointerException.
This Optional Class is included in Util Package which is to avoid
Null pointer Exception.
If the value is present it will return true otherwise it will shows the
false.
Generally in java language, to access an object we use reference type. We set the value of the reference null,when we are unable to have specific object to point to. That means it is not pointing to any object. It provides lot of useful methods so to explicit null checks have no excuse any more.
Please prefer Official java 8 documentation for more details.
Here I am going to take a small example of Optional Usage:
with nullable value and with the value which does not allow nulls
with nullable value and with the value which does not allow nulls
Example:
Optional<String> fullname=Optional.ofNullable(null);
System.out.println("full name is set ?"+fullname.isPresent());
System.out.println("full name :"+fullname.orElseGet(()-->"[None]"));
System.out.println("full name.map("Hey"+s+"!").orElse("Hey Stranger!"));
Here isPresent() method returns true if this instance of Optional has non-null value and false otherwise.Here orElseGet() method provides the fallback mechanism in case Optional has null value by accepting the function to generate by default one.Here Map() method transforms the current Optional value and returns new Optional value. Here orElse() method similar to orElseGet() but instead of function it accepts by default one.
Here is output of this program:
full name is set ?: false
full name: [None]
Hey Stranger!
Parallel Arrays:
Java 8 release adds a lot of new methods to allow parallel Array Processing.
The most important one is parallelSort() which is speedup sorting the multicore machines.
The fallowing is simple example how to use parallelSort()method in Java Program:
import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
public class ParallelArrays {
public static void main( String[] args ) {
long[] arrayOfLong = new long [ 20000 ];
Arrays.parallelSetAll( arrayOfLong,
index--->ThreadLocalRandom.current().nextInt( 1000000 ) );
index--->ThreadLocalRandom.current().nextInt( 1000000 ) );
Arrays.stream( arrayOfLong ).limit( 10 ).forEach(
i--->(System.out.print( i + " " ) ); System.out.println();
i--->(System.out.print( i + " " ) ); System.out.println();
Arrays.parallelSort( arrayOfLong );
Arrays.stream( arrayOfLong ).limit( 10 ).forEach(
i--->(System.out.print( i + " " ) );
i--->(System.out.print( i + " " ) );
System.out.println();
}
}
Note: This code uses parallelSetAll() to fill up arrys with 20000 random values. After that parallelSort() is applied. The sample program output may look like that
Unsorted: 591217 891976 443951 424479 766825 351964 242997
Sorted: 39 220 263 268 325 607
Note: please observe here array elements are randomly generated
Lambdas Expression:
Lambdas is biggest and most awaited language change in the whole java 8 release.
They allows us to treat functionality as method arguments(passing function around)
or treat a code as data.In simple form, a lambdas could be represented as a comma separated list of parameters .
the -----> symbol and the body
Example:
Arrays.asList('a','b','c').foreach((String e)--->System.out.println(e));
In case Lambdas body more complex it may be wrapped into square brackets
Arrays.asList('a','b','c').foreach(( String e)--->{
System.out.println(e);
System.out.println(e);
}
No comments:
Post a Comment