New features of Java 8

Keywords: Java Lambda Programming Big Data

Java 8 new features with Example
https://www.youtube.com/playlist?list=PLsyeobzWxl7qbvNnJKjYbkTLn2w3eRy1Q


First, you can write method body in the interface
When a new method is added to the interface, a method body can be brought along.

Benefits:
When the new interface is released, the classes that previously implemented the interface need not be changed.
Namely, there is no need to implement the newly added method.

Syntax: Use default keywords
  1. interface A{  
  2.     void show();  
  3.     default void sayHello(){  
  4.         System.out.println("Hello, World!");  
  5.     }  
  6. }  


Be careful:
1. When implementing multiple interfaces, there is implementation of method bodies with duplicate names in the interfaces.

If two interfaces with the same method name are implemented simultaneously,
You need to provide your own implementation of this method. Only then can the conflict be resolved.

sayHello() duplicates, class C needs to implement its own sayHello()
  1. interface A{  
  2.     void showA();  
  3.     default void sayHello(){  
  4.         System.out.println("Hello, A!");  
  5.     }  
  6. }  
  7. interface B{  
  8.     void showB();  
  9.     default void sayHello(){  
  10.         System.out.println("Hello, B!");  
  11.     }  
  12. }  
  13.   
  14. class C implements A,B{  
  15.   
  16.     public void sayHello(){  
  17.         System.out.println("Hello, C!");  
  18.     }  
  19.   
  20.     public static void main(String[] args){  
  21.         C c = new C();  
  22.         c.sayHello(); // Hello, C!  
  23.     }  
  24. }  

     
Priority of Class Method over Interface Method

The implementation of method sayHello() is defined in both interface A and class B.
Class C prefers the method of class B.

  1. interface A{    
  2.     void showA();    
  3.     default void sayHello(){    
  4.         System.out.println("Hello, A!");    
  5.     }    
  6. }  
  7.   
  8. class B{  
  9.     public void sayHello(){    
  10.         System.out.println("Hello, B!");    
  11.     }    
  12. }    
  13.     
  14. class C extends B implements A{   
  15.     
  16.     public static void main(String[] args){    
  17.         C c = new C();    
  18.         c.sayHello(); // Hello, B!    
  19.     }    
  20. }  


3. Methods in java.lang.Object cannot be overridden in interfaces
  1. interface A {  
  2.   
  3.     // can't define a equals method in interface.  
  4.     default public boolean equals(){  
  5.          
  6.     }  
  7. }  


4. static methods can be defined in interfaces
  1. interface A{    
  2.     void showA();    
  3.     static void sayHello(){    
  4.         System.out.println("Hello, A!");    
  5.     }    
  6. }  
  7.   
  8. class C {  
  9.     public static void main(String[] args){    
  10.         A.sayHello(); // Hello, A!  
  11.     }    
  12. }  



Functional Programming VS. Object Oriented Programming
  
   Functional Programming with Java 8
   https://www.youtube.com/watch?v=LcfzV38YDu4

Functional Programming refers to the use of Lambda expressions for interfaces with only one method.

A good example of an interface with only one method is the java.lang.Comparable interface.
It has only one method: compareTo(Object obj)



3. Lambda Expression (-> Look sideways)



Lambda Expression VS. Anonymous inner class
For implementation classes with only one method interface, when writing implementation classes, you only need to provide (parameter + Lambda + method body).

  1. interface A{  
  2.     void show();  
  3. }  
  4.   
  5. public class LambdaDemo{  
  6.     public static void main(String[] args){  
  7.         A obj;  
  8.           
  9.         // old  
  10.         obj = new A(){  
  11.             public void show(){  
  12.                 System.out.println("Hello");  
  13.             }  
  14.         }  
  15.   
  16.         // java 8  
  17.         obj = () -> {  
  18.             System.out.println("Multiple");  
  19.             System.out.println("Lines ... ");  
  20.         }  
  21.   
  22.         // or  
  23.         obj = () -> System.out.println("Single line.");  
  24.   
  25.     }  
  26. }  


Explain:



4. Adding forEach() method to java.lang.Iterable interface

NOTE:

1. Before java 8, there was only one way for the java.lang.Iterable interface:
                java.util.Iterator<T> iterator()

2. The java.util.Collection interface inherits the java.lang.Iterable interface.

  1. import java.util.Arrays;  
  2. import java.util.List;  
  3. import java.util.function.Consumer;  
  4.   
  5.   
  6. class IConsumer implements Consumer<Integer>{  
  7.     @Override  
  8.     public void accept(Integer t) {  
  9.         System.out.println(t);  
  10.     }  
  11. }  
  12.   
  13.   
  14. public class Java8ForEach {  
  15.    
  16.     public static void main(String[] args) {  
  17.         List<Integer> list =Arrays.asList(1,2,3,5,6);  
  18.            
  19.         // normal loop  
  20.         for(Integer i : list){  
  21.             System.out.println(i);  
  22.         }  
  23.            
  24.         // Java 8 forEach - normal  
  25.         Consumer<Integer> action = new IConsumer();  
  26.         list.forEach(action);  
  27.            
  28.         // Java 8 forEach - use lambda expressions.  
  29.         // see how we do it in one liner  
  30.         list.forEach(each -> System.out.println(each));  
  31.            
  32.     }  
  33.        
  34.   
  35. }  



V. Streaming API

Java collections got a new package java.util.Stream.
classes in this package provide a stream API.
And supports functional-style operations on streams of elements.

Stream API enables bulk operations like sequential or parallel map-readuce on Collections.


  1. //Java 7 or earlier:  
  2. public List listFilter(List<Integer> bigList){  
  3.     List<Integer> filteredList = new ArrayList<>();  
  4.     for (Integer p : bigList)  {  
  5.         if (p  > 40) {  
  6.             filteredList.add(p);  
  7.         }   
  8.     }  
  9.     return filteredList;  
  10. }  

  1. //Java 8:  
  2. public List listFilter(List<integer> bigList){  
  3.     return bigList  
  4.             .stream()  
  5.             .filter(p -> p > 40)  
  6.             .collect(Collectors.toList());  
  7. }  



-

So, if you know, in this world, it's an information age, we have a concept of "Big Data", "Haddoop", we have to process huge amount of data.

1) In stream we have two types of methods:

1. Intermediate method. Like: filter(), map()
   lazy, it can't give result immediately until you call a terminate method.

2. Terminate method. Like: findFirst(), forEach()

Example Given:
  1. List<Integer> values = Arrays.asList(4,5,6,7,8);  
  2. values.stream().filter(i->{  
  3.     Sysout.out.println("Hello");  
  4.     return true;  
  5. });  



2) Stream is once used, it can't be reused:

  1. Stream<Integer> s = values.stream();  
  2. s.forEach(Sysout.out::println);  // works  
  3. s.forEach(Sysout.out::println);  // throws Exception  






More:

New features of Java 9
http://programtalk.com/java/java-9-new-features/



Quote:
Java 8 Stream API Features Lambda Expression
https://www.youtube.com/playlist?list=PLsyeobzWxl7otduRddQWYTQezVul0xIX6


http://programtalk.com/java/java-8-new-features/

Posted by rocketsprocket on Tue, 09 Jul 2019 18:18:41 -0700