Hi, everyone. I’m an e-bike racer, an ordinary programming girl.

preface

Yesterday, Lambda won the anonymous class, recall, win where? Simplicity wins. Today, he turned his face and was compared. So let’s see what method reference is? This paper mainly discusses the following three points:

  • What is a method reference

  • How do I build method references

  • Comparative advantage of method references

  • conclusion

1. What are method references

Method references let you create Lambda expressions based on existing method implementations. Think of it as a shorthand for calling a Lambda for a particular method. Displays the name of the specified method, making the code more readable.

  • Method reference format

    Target reference :: Method name column User::getAgeCopy the code

,

  • Method references

    User user1 = User.of("Huang Xiaoying".18);
    User user2 = User.of(Sailor Moon.100);
    ArrayList<User> users = Lists.newArrayList(user1, user2);
    
    / / lambda method
    users.sort((a, b) -> Integer.compare(a.getAge(),b.getAge()));
    // Method reference
    users.sort(Comparator.comparingInt(User::getAge));
    
    BiPredicate<List<String>, String> contains =
                    (list, element) -> list.contains(element);
    
    BiPredicate<List<String>, String> contains = List::contains;
    
    Copy the code

    Method reference User:: getAge is equivalent to User User -> user.getage ();

2. How to construct method references

2.1 References to static methods

  Integer::parseInt
Copy the code

A method reference to an instance method of any type

(String s)-> s.length(); Equivalent to String::length (String s)-> s.toupperCase (); Equivalent to the String: : toUpperCaseCopy the code

A method that refers to an object that is itself an argument to Lambda

A method reference to an instance method of an existing object

// The user object has a test method
User user = User.of(Sailor Moon.100); users.forEach(()->user.test()); User is the original declared object users.foreach (user ::test);Copy the code

Call a method in an existing external object in a Lambda.

2.4 Constructor creation

BiFunction<String,Integer,User> User = User::new; User u = user.apply("hello",18);Copy the code

3. Comparative advantages

The following is a demonstration of sorting users by age

  1. Implement the Comparator interface and rewrite the comparison method

    
    /**
     * @author huang1996
     * @date 2021-07-14 19:52
     */
    public class MethodReference  implements Comparator<User> {
    
        @Override
        public int compare(User user1,User user2) {
            return Integer.compare(user1.getAge(),user2.getAge());
        }
    }
    Copy the code
  2. An anonymous class

    Collections.sort(users, new Comparator<User>() { @Override public int compare(User o1, User o2) { return Integer.compare(o1.getAge(),o2.getAge()); }});Copy the code
  3. Lambda expressions

     users.sort((a, b) -> Integer.compare(a.getAge(), b.getAge()));
    
    Copy the code
  4. Method references

    users.sort(Comparator.comparingInt(User::getAge));
    Copy the code

4. To summarize

From the comparison of advantages in Point 3, it is clear that the code is getting cleaner and clearer. This is the perfect point for method references.

So whenever a method reference is cleaner and cleaner, use a method reference; If method references are not concise, stick with Lambda.

Reading the source

“Java 8 In Action” chapter 3 method citation

Effective Java, article 42