lambda expressions, java double colon (: :) Detailed Example

Double colon (: :) use forms mainly comprising:

Class name :: instance methods

:: Object instance methods

Through the sample code below illustrates in detail.

Double colon (: :) function and arrows (->) collectively shown below:

Such as: HashMap :: new equivalent to () -> new HashMap ()

. 1  public  class the Test {
 2  
. 3      // instance object references an instance method 
. 4      Supplier <String> supplier1 = "lowerCase" :: the toUpperCase;
 . 5      Supplier <String> supplier1_1 = () -> "lowerCase" .toUpperCase ();
 . 6  
. 7      / / class reference (without parameters) constructor 
. 8      supplier2 Supplier :: = String <String> new new ;
 . 9      Supplier <String> supplier2_1 = () -> new new String ();
 10  
. 11      // class references (with parameters) constructor 
12      Function <String, String> function1 :: = String new new ;
 13 is     Function <String, String> function1_1 = (String STR) -> new new String (STR);
 14  
15      // class reference instance method, the instance of the object passed as parameters, the parameters, the same parameter types 
16      Function <String, String > = function2 String the toUpperCase ::;
 . 17      Function <String, String> function2_1 = (String STR) -> str.toUpperCase ();
 18 is  
. 19      // the Predicate <T> be understood as a special Function <T, Boolean> 
20 is  
21 is      Person = the Person new new the Person ();
 22 is      // shall no static method reference 
23 is      Supplier supplierBln = <Boolean> the Person :: isTest;
 24      Supplier <Boolean> supplierBln_1 = () ->Person.isTest ();
 25  
26 is      // instance object method call instance 
27      Supplier <String> supplierStr = Person :: getName;
 28      Supplier <String> supplierStr_1 = () -> person.getName ();
 29  
30      // no arguments The constructor 
31 is      Supplier <the Person> = supplierPerson the Person :: new new ;
 32      Supplier <the Person> supplierPerson_1 = () -> new new the Person ();
 33 is  
34 is      // there argument constructor 
35      BiFunction <String, String, the Person> = the Person biFunction :: new new ;
 36     BiFunction<String, String, Person> biFunction_1 = (name, gender) -> new Person(name, gender);
37 
38     // 类名调用实例方法,入参为传入实例对象
39     Function<Person, Person> functionP = Person::toOpposite;
40     Function<Person, Person> functionP_1 = person -> person.toOpposite();
41 
42     Consumer<String> consumer = System.out::println;
43     Consumer<String> consumer_1 = (String str) -> System.out.println(str);;
44 
45     public static void main(String[] args) {
46         List<String> list = Arrays.asList("1", "2", "3");
47         boolean bl = list.stream().anyMatch("1"::equals);
48         List<String> retval = list.stream().collect(Collectors.toCollection(LinkedList::new));
49 
50         List<Person> persons = Arrays.asList(new Person(10, "Jack", "M"));
51         Person person = new Person(20, "Lily", "F");
52         persons.stream().filter(Person::isMale).filter(person::isUnder).collect(Collectors.toCollection(ArrayList::new));
53     }
54 }

 

Person class code as follows:

 1 public class Person {
 2     int age;
 3     String name;
 4     String gender;
 5 
 6     public Person() {
 7     }
 8 
 9     public Person(String name) {
10         this.name = name;
11     }
12 
13     public Person(String name, String gender) {
14         this.name = name;
15         this.gender = gender;
16     }
17 
18     public Person(int age, String name, String gender) {
19         this.age = age;
20         this.name = name;
21         this.gender = gender;
22     }
23 
24     public String getName() {
25         return this.name;
26     }
27 
28     public Person toOpposite() {
29         if (gender.charAt(0) == 'M')
30             gender = "F";
31         else
32             gender = "M";
33         return this;
34     }
35 
36     public static boolean isTest() {
37         return true;
38     }
39 
40     public boolean isUnder(Person person) {
41         return person.age > this.age;
42     }
43 
44     public boolean isMale() {
45         return gender.equals("M");
46     }
47 }

 

Guess you like

Origin www.cnblogs.com/blouson/p/Java_colon_operator.html
Recommended