Java 8 new Features-Rookie tutorial (2)-java 8 method reference

Source: Internet
Author: User

Java 8 Method Reference

The method reference refers to a method by its name.

Method references can make language constructs more compact and concise, reducing redundant code.

The method reference uses a pair of colons ::.

Below, we have defined 4 methods in the Car class as examples to differentiate the references of 4 different methods in Java.

 PackageJava8.cainiao; @FunctionalInterface Public InterfaceSupplier<t>{T get ();}classCar {//supplier is the interface of jdk1.8, which is used in conjunction with LAMDA.     Public StaticCar Create (FinalSupplier<car>supplier) {        returnSupplier.get (); }      Public Static voidCollide (Finalcar car) {System.out.println ("Collided" +car.tostring ()); }      Public voidFollow (FinalCar Another) {System.out.println ("Following the" +another.tostring ()); }      Public voidrepair () {System.out.println ("Repaired" + This. toString ()); }}

1. Constructor reference: Its syntax is class::new, or the more general class< T >::new instance is as follows:

Final Car car = car.create (car::new  ); Final list< car > Cars = arrays.aslist (car);

2. Static method Reference: Its syntax is Class::static_method, with the following example:

Cars.foreach (Car::collide);

3. Method reference for any object of a particular class: its syntax is the Class::method instance as follows:

Cars.foreach (Car::repair);

4. method Reference for a particular object: its syntax is the Instance::method instance as follows:

Final Car police = car.create (car::new  ); Cars.foreach (police::follow);
Method Reference Instance

Enter the following code in the Java8tester3.java file:

 PackageJava8.cainiao;Importjava.util.List;Importjava.util.ArrayList; Public classJava8tester3 { Public Static voidMain (String args[]) {List<String> names =NewArraylist<>(); Names.add ("Google"); Names.add ("Runoob"); Names.add ("Taobao"); Names.add ("Baidu"); Names.add ("Sina"); /*** In the example we will System.out::p Rintln method as a static method to reference*/Names.foreach (system.out::p rintln); }}

In the example we will System.out::p Rintln method as a static method to refer to.

Execute the above script and the output is:

Googlerunoobtaobaobaidusina

Java 8 new Features-Rookie tutorial (2)-java 8 method reference

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.