What is a method reference

A method reference is just the name of the method, and the actual invocation is handed over to the functional interface, which needs to be used with Lambda expressions.

Such as:

List<String> list = Arrays.asList("a","b","c");
list.forEach(str -> System.out.print(str));
list.forEach(System.out::print);
Copy the code

The two notations above are equivalent.

Method reference classification

Constructor method references

Format: Class::new, calling the default constructor.

Class static method references

Format: Class: : static_method

Class ordinary method references

The format is Class::method. The method cannot take arguments.

4. Instance method references

Format: the instance: : method

The sample

Add a method reference to the User class:

public static User create(Supplier<User> supplier){
    return supplier.get();
}

public static void updateUsername(User user){
    user.setUsername(user.getUsername() + " updated.");
}

public void updateAge(){
    this.setAge(this.getAge() + 10);
}

public void changeAge(User user){
    user.setAge(user.getAge() + 10);
}
Copy the code

Method reference test:

public static void main(String[] args) { List<User> list = initList(); NewUser = user.create (User::new); newUser.setAge(1); newUser.setUsername("new"); System.out.println(newUser); ForEach (User::updateUsername); ForEach (User::updateAge); User User = new User(); list.forEach(user::changeAge); list.forEach(System.out::println); } private static List<User> initList() { List<User> list = new ArrayList<>(); list.add(new User("oaby", 23)); list.add(new User("tom", 11)); list.add(new User("john", 16)); list.add(new User("jennis", 26)); list.add(new User("tin", 26)); list.add(new User("army", 26)); list.add(new User("mack", 19)); list.add(new User("jobs", 65)); list.add(new User("jordan", 23)); return list; }Copy the code

Output result:

User [username=new, age=1]
User [username=oaby updated., age=43]
User [username=tom updated., age=31]
User [username=john updated., age=36]
User [username=jennis updated., age=46]
User [username=tin updated., age=46]
User [username=army updated., age=46]
User [username=mack updated., age=39]
User [username=jobs updated., age=85]
User [username=jordan updated., age=43]
Copy the code

You can see that the method references are valid, and username and age are updated accordingly.

Recommended reading

Dry goods: 2TB architect four-stage video tutorial

Interview: the most complete Java multithreaded interview questions and answers

Interview: the most comprehensive ali advanced Java interview questions in history

Interview: The most complete Spring interview questions in history

Tutorial: The most complete Spring Boot complete video tutorial

Books: 15 must-read books for advanced Java architects

Tools: Recommended an online creation flow chart, mind mapping software

Share Java dry goods, high concurrency programming, hot technology tutorials, microservices and distributed technology, architecture design, blockchain technology, artificial intelligence, big data, Java interview questions, and cutting-edge hot news.