Introduction to new features of Java8 (Java 8 new features)

Source: Internet
Author: User
Tags iterable stream api java 8 stream
Java 8 new features with Example
Https://www.youtube.com/playlist?list=PLsyeobzWxl7qbvNnJKjYbkTLn2w3eRy1Q


One, you can write the method body in the interface
When you add a new method to an interface, you can bring the method body.

The benefits:
When a new interface is published, the classes that previously implemented the interface do not need to be changed.
That is, you do not need to implement a newly added method.

Syntax: Using defaultKey words
Java code interface a{void Show ();       default void SayHello () {System.out.println ("Hello, world!"); }   }

Attention:
1, implementation of multiple interfaces, the interface has a duplicate method of the implementation.

If two interfaces that have the same method name and implement the method are implemented simultaneously,
You need to provide your own implementation of this method. In order to resolve the conflict.

SayHello () Repeat, class C needs to implement its own SayHello ()
Java code   interface a{       void showa ();        default void sayhello () {            System.out.println ("hello, a!");        }  }   interface b{        VOID SHOWB ();       default void sayhello () {           system.out.println ("hello, b!");        }  }      class c implements a, b{          public void sayhello () {            system.out.println ("hello, c!");        }          public static  Void main (string[] args) {           c c = new c ();            c.sayhello (); // hello, c!       }  }  

2, the class method priority is higher than the interface method

The implementation of Method SayHello () is defined in both interface A and class B.
Class C takes precedence over the method of class B.

Java code   interface a{         void showa ();          default void sayhello () {              system.out.println ("hello, a!");          }    }      class b {       public void sayhello () {              system.out.println ("hello, b!");          }    }           class c extends b implements a{             public static void main (String[] args) {             c c = new c();             c.sayhello ();  // Hello,  B!         }    }  

3, the interface can not rewrite the java.lang.Object inside the method
Java code interface A {//can ' t define a equals method in interface. Default public boolean Equals () {}}

4, you can define the static method in the interface
Java code interface a{void ShowA ();         static void SayHello () {System.out.println ("Hello, a!");       Class C {public static void main (string[] args) {A.sayhello ();//Hello, A! }     }


second, functional programming VS. Object Oriented Programming

functional Programming with Java 8
Https://www.youtube.com/watch?v=LcfzV38YDu4

Function-oriented programming (functional programming) means that an interface with only one method can be implemented using a LAMBDA expression.

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



Third, Lambda Expression (-> oblique look)



Lambda Expression VS. Anonymous Inner class
For the implementation class of an interface with only one method, the write implementation class simply provides (parameter + Lambda + method body).

Java code   interface a{       void show ();  }      public class lambdademo{       public static void  main (String[] args) {           A obj;                        // old           obj = new a () {                public void show () {                    system.out.println ("Hello");                }           }               // java 8           obj  =  ()  -> {                SYSTEM.OUT.PRINTLN ("multiple");                system.out.println ("lines ... ");            }              // or           obj =  ()  -> system.out.println ("single  line. ");           }  }  

Description



Iv. Adding a ForEach () method for the Java.lang.Iterable interface

Note:

1, Java 8, the Java.lang.Iterable interface has only one method:
Java.util.iterator<t> iterator ()

2, Java.util.Collection interface, inherits the Java.lang.Iterable interface.

Java code   import java.util.arrays;   import java.util.list;   import  java.util.function.consumer;         class iconsumer implements  consumer<integer>{        @Override         Public void accept (integer t)  {            system.out.println (t);       }  }          public class java8foreach {           public  static void main (String[] args)  {            list<integer> list =arrays.aslist (1,2,3,5,6);                        // normal  loop          for (integer i : list) {                system.out.println (i);            }                        // java 8 foreach - normal            Consumer<Integer> action =  New iconsumer ();           list.foreach (action);                         // Java 8 forEach - use lambda expressions.            // see how we do it in one  liner   &NBSp;       list.foreach (Each -> system.out.println (each));                    }              }  


Five, 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.

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


Java Code//java 7 or Earlier:public List listfilter (list<integer> biglist) {list<integer> FilteredLis       t = new arraylist<> ();           for (Integer p:biglist) {if (P >) {filteredlist.add (P);   } return filteredlist; }
Java code//java 8:public List listfilter (list<integer> biglist) {return biglist. Stream ()   . Filter (P-> p >). Collect (Collectors.tolist ()); }


-

So, if you are know, in this world, it's a information age, we have a concept's "big Data", "Haddoop", we have to process Hu GE 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 your call a Terminate method.

2. Terminate method. Like:findfirst (), ForEach ()

Example Given:
Java code list<integer> values = arrays.aslist (4,5,6,7,8);       Values.stream (). Filter (i->{Sysout.out.println ("Hello");   return true; });


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

Java code stream<integer> s = Values.stream ();  S.foreach (sysout.out::p rintln);  Works S.foreach (sysout.out::p rintln); Throws Exception





More:

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



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


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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.