Java 8 Features

1.1 Lambda Expressions


# Represent one method interface using an expression

# An interface which has only one abstract method is called functional interface (@FunctionalInterface)

# It helps to iterate, filter and extract data from collection

# Lambda expression is treated as a function

# (argument-list) -> {body}  

1.2 Lambda example


    @FunctionalInterface
    interface Addable{  
        int add(int a,int b);  
    }  
      
    public class LambdaExpressionExample6 {  
        public static void main(String[] args) {  
              
            // Lambda expression without return keyword.  
            Addable ad1=(a,b)->(a+b);  
            System.out.println(ad1.add(10,20));  
              
            // Lambda expression with return keyword.    
            Addable ad2=(int a,int b)->{  
                                return (a+b);   
                                };  
            System.out.println(ad2.add(100,200));  
        }  
    }  

1.2 Lambda - List


    List<String> list=new ArrayList<String>();  
    list.add("ankit");  
    list.add("mayank");  
    list.add("irfan");  
    list.add("jai");  
      
    list.forEach(  
        (n)->System.out.println(n)  
    );     

1.3 Lambda - comparator


    Collections.sort(list,(p1,p2)->{  
        return p1.name.compareTo(p2.name);  
      });  

1.4 Lambda - filter


    // using lambda to filter data  
    Stream<Product> filtered_data = list.stream().filter(p -> p.price > 20000);  

2.1 Method References


# Used to refer method of functional interface
# Reference to a static/instance/constructor method.
# It is compact and easy form of lambda expression

2.1 Method References - Static Method


    # ContainingClass::staticMethodName

    interface Sayable{  
        void say();  
    }  

    public class MethodReference {  
        public static void saySomething(){  
            System.out.println("Hello, this is static method.");  
        }  
        public static void main(String[] args) {  
            // Referring static method  
            Sayable sayable = MethodReference::saySomething;  
            // Calling interface method  
            sayable.say();  
        }  
    }  

2.2 Method References - Instance Method


    # containingObject::instanceMethodName 

    interface Sayable{  
        void say();  
    }  

    public class InstanceMethodReference {  
        public void saySomething(){  
            System.out.println("Hello, this is non-static method.");  
        }  
        public static void main(String[] args) {  
            // Creating object  
            InstanceMethodReference methodReference = new InstanceMethodReference(); 
                // Referring non-static method using reference  
                Sayable sayable = methodReference::saySomething;  
                sayable.say();  

                // Referring non-static method using anonymous object  
                Sayable sayable2 = new InstanceMethodReference()::saySomething;  
                sayable2.say();  
        }  
    }  

2.3 Method References - Constructor


    # ClassName::new  

    interface Messageable{  
        Message getMessage(String msg);  
    }  

    class Message{  
        Message(String msg){  
            System.out.print(msg);  
        }  
    }  
    
    public class ConstructorReference {  
        public static void main(String[] args) {  
            Messageable hello = Message::new;  
            hello.getMessage("Hello");  
        }  
    }   

3.1 groupingBy Collectors


    # similar to the ‘GROUP BY' clause in the SQL language
    // data - list of Blogs
    // getCreatedOn - getter function for attribute of class Blogs 
    // getViews     - getter function for attribute of class Blogs 

    import java.util.stream.Collectors;
    Map<LocalDate, Long> createdCount = data.stream()
        .collect(Collectors.groupingBy(Blogs::getCreatedOn, Collectors.counting()));

    Map<LocalDate, Integer> viewedCount = data.stream()
        .collect(Collectors.groupingBy(Blogs::getCreatedOn, Collectors.summingInt(Blogs::getViews)));


    averagingInt(BlogPost::getLikes)
    summingInt(BlogPost::getLikes)
    maxBy(comparingInt(BlogPost::getLikes))
    summarizingInt(BlogPost::getLikes)

    JAVA 8 - Grouping By

4.1 forEach() method in Iterable interface


JAVA 8 - foreach