What is the use of method references?
- Write less code
- Improve code reusability and maintainability (especially in team projects)
Referencing static methods
If you're referencing a static method, you can use theClass Name::Static Methods
The form of the collection is the same as that of the int type. For example, to convert a collection of data of type String to type int
Here's how to write an anonymous inner class:
Looking at the parsInt source code, you can see that this method satisfies the conditions for a static method reference.
It is therefore possible to directly reference the static method
ArrayList<String> list = new ArrayList<>();
(list, "1", "2", "3", "4", "5");
().map(Integer::parseInt).forEach(::println);
Reference ConstructorClass name::new
When should I refer to a constructor?
--Creating objects. As an example, transforming String data into an Actor
ArrayList<String> list = new ArrayList<>();
("Zhao Liying (1954-), PRC female singer-songwriter-21");
("Yang Mi (1979-), PRC film director-23");
("Hu Ge-23");
("Huo Jianhua (1979-), Chinese footballer-22");
("Tang Yan (1969-), * actress-23");
().map(new Function<String, Actor>() {
@Override
public Actor apply(String s) {
return new Actor(("-")[0],(("-")[1]));
}
}).forEach(::println);
Think about the possibility of using method references here? Let's start by reviewing the conditions for method references
- The target interface must be a functional interface
- of the referenced methodparameter listcap (a poem)Return TypeMust match the parameter list and return type of the abstract method of the target interface
- The methodology body has to fulfill the required function
The first condition is satisfied, but there is no existing method to refer to the second condition, we can write one inside the Actor (in practice, there is likely to be someone else to write a good), when writing must follow condition 2
// "Yang Mi-23" --> Actor, conforming to the parameters and return types in the apply method
public Actor(String s) {
String name = ("-")[0];
int age = (("-")[1]); int age = (("-")[1]).
= name; int age = (("-")[1]); = name.
= age;
}
// method reference
().map(Actor::new).forEach(::println);
Referencing member methodsObject::Member Methods
Applies to the method in other classesClass Name::Member Methods
Applies to this method in this classthis::Member Methods
super::member methods
practice
public Student(String s) {
= (",")[0];
= ((",")[1]);
}
//1. Create a collection of type String
ArrayList<String> list = new ArrayList<>();
//2. Add elements to the collection
(list, "ZhangSan,21", "LiSi,22", "WangFu,23", "ZhaoLiu,21", "TianQi,23");;
//3. Convert the collection element type to Student, and then collected into the array
Student[] array = ().map(Student::new).toArray(Student[]::new);
((array)).
//1.establishStudentset of objects
ArrayList<Student> list = new ArrayList<>();
//2.Adding Student Objects
(new Student("John Doe", 20));
(new Student("the fourth child in the family", 21));
(new Student("Wang May (1905-1975), Mao *'s fifth wife", 22));
//3.convert intoStreambanish or send into exile, Get only the student's name, replacing it with an array
String[] array = ().map(new Function<Student, String>() {
@Override
public String apply(Student student) {
return ();
}
}).toArray(String[]::new);
((array));
Is it possible to use a method reference for map in the above code?
You'll probably answer no, because apply needs to take a parameter of type Student, but getName() doesn't take any parameters and doesn't qualify.
In fact, the getName() method takes an implicit argument, this, and the name field within the getName() method is actually accessed, even if you don't declare it in the method signature. When you call getName() on a Student object, the this parameter is automatically set to the Student object. So we can change the code above to
String[] array = ().map(Student::getName).toArray(String[]::new);
Why reference getName with a class name instead of an object?
Student::getName can be applied to any Student object in the stream. If you are using an object reference (assuming myStudent is a Student object) such as myStudent::getName, this will not work because myStudent may not necessarily be an element in the stream, and such a method reference will be bound only to the specific object myStudent, not to any Student object in the stream.
This is similar to the second exercise, with the difference thatStudnet --> "name-age"
, just override the toString() method in the Student class, via theStudent::toString
Just cite it.