23 April, 2022

 java8 new features:

1) Consumer<T> :Its a part of java.util.function package. Consumer can be used in all contexts where an object needs to be consumed,i.e. taken as input, and some operation is to be performed on the object without returning any result. 

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

import java.util.function.Consumer;


public class ConsumerFunctionEx{

  public static void main(String args[]){

    Consumer<Integer> consumer= i-> System.out.print(i+",");

    List<Integer> integerList=Arrays.asList(new Integer(1), 

                              new Integer(10), new Integer(200),

                              new Integer(101), new Integer(-10),

                              new Integer(0));

    print(integerList, consumer);

 }

 public static void print(List<Integer> listOfIntegers, Consumer<Integer> consumer){

  for(Integer integer:listOfIntegers){

    consumer.accept(integer);

  }

 }

}

================

output:

1,10,200,101,-10,0,


2)Functional Interface : it contains only one abstract method.

Functional interfaces are also known as Single Abstract Method Interfaces (SAM Interfaces).
@FunctionalInterface
interface add_cal{
    int add(int a, int b);
}
public class Sample{
    public static void main(String[] args){
        add_cal a1 = (a,b) -> {
            return a+b;
        };
        System.out.println(a1.add(5,10));
    }
}
============
output
15

3) Optional Class :used to deal with NullPointerException

import java.util.Optional;  
public class OptionalExample {  
    public static void main(String[] args) {  
        String x = "hello";
        Optional<String> opt = Optional.ofNullable(x);
        if(opt.isPresent()){
            System.out.println(opt.get());
        }
    }  

===============
output:
hello

4) forEach loop:new method forEach() to iterate the elements
public class ForEachExample {  
    public static void main(String[] args) {  
        List<String> gamesList = new ArrayList<String>();  
        gamesList.add("Football");  
        gamesList.add("Cricket");  
        gamesList.add("Chess");  
       gamesList.forEach(games -> System.out.println(games));  
          
    }  
}  

=============
output:
Football
Cricket
Chess

5) Default Methods :defined inside the interface and tagged with default are known as default methods. These methods are non-abstract methods. This has implementation.

interface Sayable{  
    default void say(){  
        System.out.println("Hello, this is default method");  
    }  
}  
public class DefaultMethods implements Sayable{  
    public static void main(String[] args) {  
        DefaultMethods dm = new DefaultMethods();  
        dm.say();   
     }  
}

6) StringJoiner: 
sed to construct a sequence of characters separated by a delimiter
public class StringJoinerExample {  
    public static void main(String[] args) {  
        StringJoiner joinNames = new StringJoiner(","); 
        joinNames.add("hello");  
        joinNames.add("world");  
        
        System.out.println(joinNames);  
    }  

=====
output: hello,world

7) Collectors : It provides reduction operations
import java.util.stream.Collectors;  
import java.util.List;  
import java.util.ArrayList;  

public class CollectorsExample {  
    public static void main(String[] args) {  
        List<Integer> list = new ArrayList<Integer>(); 
        List<Integer> list1 = new ArrayList<Integer>(); 
        list.add(1);
        list.add(2);
        list.add(3);
        list1 = list.stream().map(x->x*x).collect(Collectors.toList());
        System.out.println(list1); 
        
    }  
============
output: [1, 4, 9]

8)Stream: does not store elements.lazy and evaluates code only when required
public class StreamExample {  
    public static void main(String[] args) {  
        List<Integer> list = new ArrayList<Integer>(); 
        List<Integer> list1 = new ArrayList<Integer>(); 
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list1 = list.
        stream().filter(x->x%2==0).
        map(x->x*x).
        collect(Collectors.toList());
        System.out.println(list1); 
        
    }  
}
============
output: [4, 16]

9) CompletableFuture:extension of Future’s API.
Future:The get() method in the Future Interface is blocking operation.Multiple futures cannot be chained together with Future.

No comments:

Post a Comment