Java8 method Reference with constructor reference

Source: Internet
Author: User

 PackageJava_8;Importorg.junit.Test;ImportJava.io.PrintStream;ImportJava.util.Comparator;Importjava.util.function.*;Importjava.util.function.Function;/*** Method Reference: If the contents of the lambda body have a method already implemented, then we can use "method reference" * (can be understood as a method reference when the lambda expression of another form of representation * * There are mainly three syntax formats: * * Object:: Instance Method Name * * Class:: Static method Name * Class:: Instance method Name * * NOTE: * 1. The parameter list and return value type of the method called in the lambda body are consistent with the function list and return value type of the abstract method in the function interface * 2. If the first parameter in the lambda parameter list is the caller of the instance method, and the second argument is the argument to the instance method, You can use Classname::method * Second, constructor reference * Format: * classname::new * Note: The constructor method that needs to be called is consistent with the parameter list of the abstract method in the function interface * Three, array reference * Type:: New */ Public classTestmethodref {//Array References:@Test Public voidtest7 () {Function<integer, string[]> fun = xNewString[x]; String[] STRs= Fun.apply (10);        System.out.println (strs.length); Function<Integer,String[]> fun1 = string[]::New; STRs= Fun1.apply (20);    System.out.println (strs.length); }    //Constructor Reference@Test Public voidTest5 () {Supplier<Employee> sup = ()NewEmployee (); Employee EMP=Sup.get (); //Constructor Reference//Auto-match constructor based on parameter listsupplier<employee> sup2 = Employee::New; EMP=Sup2.get ();    SYSTEM.OUT.PRINTLN (EMP); } @Test Public voidTest6 () {Function<Integer,Employee> func = xNewEmployee (x); Employee EMP= Func.apply (10);        SYSTEM.OUT.PRINTLN (EMP); Function<Integer,Employee> func1 = Employee::New; EMP= Func1.apply (10);       SYSTEM.OUT.PRINTLN (EMP); //Bifunction<integer, Integer, employee> bf = employee::new; compilation error, no two Integer constructors    }    //object:: Instance method name@Test Public voidtest1 () {Consumer<String> con = xSystem.out.println (x); PrintStream PS= System.out;//Print Flow//Prerequisites: The method body parameter in consumer and the return value are the same as the parameters and return value types in the Ps.println method//consumer:void Accept (T t); t is a string here//printstream:public void println (String x)//Both the arguments passed in are string and the return value is void so satisfy that you can use the method referenceConsumer<string> Con1 =PS::p rintln; Consumer<String> con2 = System.out::p rintln;//these three ways result in the sameCon.accept ("Huang"); Con1.accept ("Huang"); Con2.accept ("Huang"); } @Test Public voidtest2 () {Employee emp=NewEmployee (); Supplier<String> sup = ()Emp.getname (); Supplier<String> sup2 =Emp::getname; }    //---------------------------------------    //class:: Static method Name@Test Public voidtest3 () {Comparator<Integer> com = (x, y)integer.compare (x, y); //Prerequisites: Same as abovecomparator<integer> com1 =Integer::compare; }    //class:: Instance method name@Test Public voidtest4 () {bipredicate<string, string> bp = (x, y)x.equals (y); BooleanBOOL = Bp.test (NewString ("Huang"), "Huang");        SYSTEM.OUT.PRINTLN (BOOL); //premise: The first parameter is the caller of the instance method, and the second argument is the argument of the instance method//For example, X is the caller of the equal method, and Y is the argument to the instance methodBipredicate<string,string> BP2 =string::equals; BOOL= Bp2.test ("Huang", "Huang");    SYSTEM.OUT.PRINTLN (BOOL); }}
 PackageJava_8; Public classEmployee {Private intID; PrivateString name; Private intAge ; Private Doublesalary;  PublicEmployee () {} PublicEmployee (intID) {         This. ID =ID; } @Override PublicString toString () {return"employee{" + "id=" + ID + ", name= ' +" + name + ' \ ' + ", age=" + Age + ", salary=" + Salary + '} '; }     PublicEmployee (String name,intAgeDoublesalary) {         This. Name =name;  This. Age =Age ;  This. Salary =salary; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     Public Doublegetsalary () {returnsalary; }     Public voidSetsalary (Doublesalary) {         This. Salary =salary; }}

Java8 method Reference with constructor 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.