Lambda Expressions
Optional
Streams
Method References
Data Time API
Nashorn Javascript Engine
Parallel Arrays
1.Default and Static methods in Interface :
- Java 8 introduces new features to interfaces.
- Before java 8 interface having only abstract methods but now java 8 added two more type of methods to interface !.
- First one is default method. A method which is having a default keyword with method body.
- Actually interfaces wont have any implemented methods but now with java 8 default method we can add a method with default implementation by using "default " keyword.
- The classes which are implementing this interface can use these default method and same time it can override the existing method. But its not mandatory to override.
- package com.instanceofjava;
- interface Java8InterfaceDemo{
- abstract void add();
- default void display(){
- System.out.println("default method of interface");
- }
- }
- The second new method introduced in java 8 is static method.
- Yes like in classes now we can define a static methods inside interface by using "static".
- Basically static methods which are defined in interface are interface level only. if we want to call these static methods which are defined in interfaces we need to use interface name so that we can access these methods.
- package com.instanceofjava;
- interface Java8InterfaceDemo{
- abstract void add();
- default void display(){
- System.out.println("default method of interface");
- }
- public static void show(){
- System.out.println("static method of interface");
- }
- }
2.Lambda Expressions
- One of the most awaited and biggest release in java 8 is lamda expressions.
- Ability to pass functionality/ behavior to methods as arguments.
- Allows us to write a method in the same place we are going to use it.
- package com.instanceofjava;
- interface JavalamdaExpression{
- public static void main(String[] args){
- Arrays.asList( "j", "a", "v" ,"a","8").forEach( e -> System.out.print( e ) );
- // java8
- }
- }
3.java.util.Optional:
- One of the best and cool feature of java 8 is Optional class. Which is a final calls from java.util package.
- The major repeating statement in every project is checking "NullPointerException". Before using any object we need to check whether it is null or not if its not null then only we need to proceed.
- Optional is just like a container which holds a value of type <T> or "null". By using isPresent() method of Optional class we can check particular object is null not not.
- package com.instanceofjava;
- import java.util.Optional:
- class Java8OptionalDemo{
- public static void main(String[] args ){
- Optional< String > str = Optional.ofNullable( null );
- System.out.println( "str having value ? " + str.isPresent() ); // output : str having value ? false
- }
- }
4.Streams:
- One of the excellent feature from java 8 as java.util.stream.
- Stream API introduces real-world functional-style programming into the Java.
- Provides functional operations on stream of elements such as list , set and map
- Supports filtering, mapping and removal of duplicates of elements in collections, are implemented lazily.
- Now we can get Streams from collections, arrays and bufferedReaders etc.
- package com.instanceofjava;
- import java.util.Arrays;
- class Java8StreamsDemo{
- public static void main(String[] args ){
- Arrays.stream(new int[] {1, 2, 3,4,5})
- .map(n -> 2 * n + 1)
- .average()
- .ifPresent(System.out::println); // output: 7.0
- }
- }
5.Method Reference:
- We can use lambda expressions to create anonymous methods.
- Sometimes, however, a lambda expression does nothing but call an existing method.
In those cases, it's often clearer to refer to the existing method by name. - Using Method references refer to the existing method by name, they are compact, easy-to-read lambda expressions for methods that already have a name
- package com.instanceofjava;
- import java.util.Arrays;
- class Java8MethodRef{
- public void show(String str){
- System.out.println(str);
- }
- public static void main(String[] args ){
- Arrays.asList("a", "b", "c").forEach(new A()::show); // a b c
- }
- }
6.Data Time API
- The next cool feature from java 8 is new date time API(jsr 310) added within java.time package.
- Before java 8 if we want to format dates we use SimpleDateFormatter class in java 8 while declaring date itself it has constructor to pass format of date.
- Some of the new classes introduced in java 8 date time are as follows.
- LocalTime
- LocalDate
- LocalDateTime
- OffsetDate
- OffsetTime
- OffsetDateTime
- package com.instanceofjava;
- import java.util.Arrays;
- class Java8DateTimeAPI{
- public static void main(String[] args ){
- LocalDate currentDate = LocalDate.now();
- System.out.println(currentDate);
- LocalDate twentyMarch2015 = LocalDate.of(2015, Month.MARCH, 06);
- System.out.println(twentyMarch2015); //2015-03-06
- LocalDate firstApril2015 = LocalDate.of(2015, 4, 1);
- System.out.println(firstApril2015);//2015-04-01
- }
- }
7.Nashorn Javascript Engine
- Java 8 come with new Nashorn Javascript Engine which is allowing us to develop and run JavaScript applications.
- package com.instanceofjava;
- import javax.script.ScriptEngine;
- import javax.script.ScriptEngineManager;
- import javax.script.ScriptException;
- import java.util.Arrays;
- class Java8JavaScript{
- public static void main(String[] args ){
- ScriptEngineManager manager = new ScriptEngineManager();
- ScriptEngine engine = manager.getEngineByName( "JavaScript" );
- System.out.println( engine.getClass().getName() );
- System.out.println( "output:" + engine.eval( "function show() { return 10; }; show();" ) );
- }
- }
- jdk.nashorn.api.scripting.NashornScriptEngine
- output:10
8.Parallel Array Sorting
- As of now java 7 we already having Arrays.sort() method to sort objects now java 8 introduced parallel sorting which has more speed than arrays.sort() and follows Fork/Join framework introduced in Java 7 to assign the sorting tasks to multiple threads that are available in the thread pool.
- Java 8 added parallel sorting functionalities to java.util.Arrays to take advantage of multithread machines
- package com.instanceofjava;
- import java.util.Arrays;
- class Java8JavaScript{
- public static void main(String[] args ){
- int arr[]={1,4,2,8,5};
- Arrays.parallelSort(arr);
- for(int i:arr){
- System.out.println(i);
- }
- }
- }
Courtesy:- instanceofjava
No comments:
Post a Comment