Java8 new Feature-method reference

Source: Internet
Author: User
Tags instance method

Method Reference: If the function in the LAMBDA body, there is already a method to provide the implementation, you can use the method reference
(You can interpret a method reference as a different representation of a LAMBDA expression)
1. Reference to Object:: Instance method name
2. Class Name:: Static method Name
3. Class Name:: Instance method name
Attention:
The ① method references the parameter list of the referenced method and the return value type, which needs to be consistent with the parameter list and return value type of the abstract method in the function interface!
② if the first parameter of the lambda parameter list is the caller of the instance method, the second argument (or no argument) is the argument of the instance method when the format is: Classname::methodname
Second, constructor reference: constructor parameter list, need to be consistent with the parameter list in the function interface!
1. Class Name:: New
Three, array reference
Type []:: New;

Array reference
   /**      * Array Reference      */     @Testpublicvoid  test01 () {        FunctionNew  string[a];         = function.apply (9);        System.out.println (strings.length);        System.out.println ("equal to");        Function<integer, string[]> function1 = string[]::new;         = function1.apply (9);        System.out.println (strings1.length);    }
Object reference:: Instance method name
/**      * Object reference:     : Instance method name */    @Test    publicvoid  test02 () {        = System.out;        Consumer<String> Consumer = (a) ps.println (a);        Consumer.accept ("aaaaaaa");        System.out.println ("equal to");        Consumer<String> consumer1 = PS::p rintln;        Consumer1.accept ("GHHASDFH");    }
Object reference:: Instance method name
    /**      * Object reference:: Instance     method name */    @Test    publicvoid  test03 () {        new Employee (101, "Zhang San", 9999.99);        Supplier<String> sup = () emp.getname ();        System.out.println (Sup.get ());        System.out.println ("----------------------------------");        Supplier<String> sup2 = emp::getname;        System.out.println (Sup2.get ());    }
Class Name:: Static method Name
// class Name:: Static method Name     @Test    publicvoid  test4 () {        Comparator<Integer> com = (x, y)- > integer.compare (x, y);        System.out.println ("-------------------------------------");        Comparator<Integer> com2 = integer::compare;    }
Class Name:: Instance method name
  //class Name:: Instance method name@Test Public voidTest5 () {bipredicate<string, string> bp = (x, y)x.equals (y); System.out.println (Bp.test ("ABCDE", "ABCDE")); System.out.println ("-----------------------------------------"); Bipredicate<string, string> bp2 =string::equals; System.out.println (Bp2.test ("ABC", "ABC")); System.out.println ("-----------------------------------------"); Function<employee, string> fun = (e)e.show (); System.out.println (Fun.apply (NewEmployee ())); System.out.println ("-----------------------------------------"); Function<employee, string> fun2 =employee::show; System.out.println (Fun2.apply (NewEmployee ())); }
Constructor reference
 Public void test7 () {        Function<string, employee> fun = Employee::new;        Bifunction<string, Integer, employee> fun2 = Employee::new;    }















Java8 New Attribute-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.