Java learning notes-method reference and java learning notes reference
Method Reference)
The Lambda expression is recorded in the previous article. You can create an anonymous method.When a Lambda expression only calls an existing method, you can use the method reference.(Features of JDK 8 ). As follows:
1 public class Person { 2 3 public enum Sex { 4 MALE, FEMALE 5 } 6 7 String name; 8 LocalDate birthday; 9 Sex gender;10 String emailAddress;11 int age;12 13 public int getAge() {14 return age;15 }16 17 public LocalDate getBirthday() {18 return birthday;19 } 20 21 public static int compareByAge(Person a, Person b) {22 return a.birthday.compareTo(b.birthday);23 }24 }
If you want to sort a group of people by age, you can use the following method to pass the personnel Array and the implemented comparator to the Array. sort method:
1 Person[] rosterAsArray = roster.toArray(new Person[roster.size()]);2 3 class PersonAgeComparator implements Comparator<Person> {4 public int compare(Person a, Person b) {5 return a.getBirthday().compareTo(b.getBirthday());6 }7 }8 9 Arrays.sort(rosterAsArray, new PersonAgeComparator());
Of course, you can use Lambda expressions to implement PersonAgeComparator, as follows:
1 Arrays.sort(rosterAsArray, (a,b) -> a.birthday.compareTo(b.birthday));
The Person class already contains compareByAge based on age,You only need to call it directly in the Lambda expression body.:
1 Arrays.sort( rosterAsArray, (a,b) -> Person.compareByAge(a,b));
Since Lambda expressions call an existing method, you can use method reference instead of Lambda expressions, as shown below:
1 Arrays.sort(rosterAsArray, Person::compareByAge);
Specifically, Person: compareByAge is equivalent to (a, B)-> Person. compareByAge (a, B. (1) its parameters are copied to Comparator <Person>. compare, that is(Person, Person); (2) the body will call Person. compareByAge.
Method reference type
There are four methods for reference, as shown below:
- Reference static methods, such as ContainingClass: staticMethodName;
- Reference the instance method, such as containingObject: instanceMethodName;
- Methods that reference special types of objects, such as ContainingType: methodName;
- Reference constructors, such as ClassName: new.
(1) Reference static methods
The example above is a static method reference.
(2) instance Reference Method
That is, the reference method through the instance of the class is as follows:
1 class ComparisonProvider { 2 public int compareByName(Person a, Person b) { 3 return a.getName().compareTo(b.getName()); 4 } 5 6 public int compareByAge(Person a, Person b) { 7 return a.getBirthday().compareTo(b.getBirthday()); 8 } 9 }10 ComparisonProvider myComparisonProvider = new ComparisonProvider();11 Arrays.sort(rosterAsArray, myComparisonProvider::compareByName);
(3) methods for referencing special types of objects
Take String as an example:
1 String[] stringArray = { "Barbara", "James", "Mary", "John",2 "Patricia", "Robert", "Michael", "Linda" };3 Arrays.sort(stringArray, String::compareToIgnoreCase);
(4) Reference Constructor
Assume that transferElements is used to copy elements from one set to another, as shown below:
1 public static <T, SOURCE extends Collection<T>, DEST extends Collection<T>> 2 DEST transferElements( 3 SOURCE sourceCollection, 4 Supplier<DEST> collectionFactory) { 5 6 DEST result = collectionFactory.get(); 7 for (T t : sourceCollection) { 8 result.add(t); 9 }10 return result;11 }
Supplier contains a get method, which returns only one empty set object without any parameters. It can be implemented using Lambda expressions, as shown below:
1 Set<Person> rosterSetLambda =2 transferElements(roster, () -> { return new HashSet<>(); });
You can reference the constructor as follows:
Set <Person> rosterSet = transferElements (roster, HashSet: new); // or Set <Person> rosterSet = transferElements (roster, HashSet <Person >:: new );
Summary
- For Lambda expressions, if you only call an existing method, you can use the method reference method. The Compiler automatically translates
Reference
Method references