1. Introduction
In this article, we’ll look at what a method reference is and how to use it.
2. Usage scenarios for method references
Let’s first look at the use of method references:
new Random().ints(10)
.map(i->Math.abs(i))
.forEach(i -> System.out.println(i));
Copy the code
Here we randomly generate 10 integers and take the absolute value of them and print them out. There’s no problem with the way you write it, but you can simplify it.
The map method accepts a functional IntUnaryOperator, so I -> math.abs (I) in the code above is actually:
new IntUnaryOperator() {
@Override
public int applyAsInt(int operand) {
returnMath.abs(operand); }}Copy the code
Abs (int I), with the same argument list, return values, and no additional logic. This is very important in order to replace each other without any other logic. Lambda expressions can then be simplified through method references. The above formula can be simplified as:
new Random().ints(10)
.map(Math::abs)
.forEach(System.out::println);
Copy the code
3. Method references
Java method references are a new feature introduced in Java 8 with Lambda expressions. You can directly reference methods or constructors of existing Java classes or objects. Method references are often used in conjunction with Lambda expressions to simplify code. It is used if the body of a Lambda expression contains only one expression and the Lambda expression calls only one existing method; The argument list and return values of the referenced method are the same as the input and output of the Lambda expression.
3.1 format
Methods refer to the format for < ClassName | instance > : : < MethodName >. The class name of the referenced method is separated from the method name by a double colon ::, except for the constructor method, which uses the new keyword.
Reference way | instructions |
---|---|
Static method reference | ClassName :: staticMethodName Like the one aboveMath::abs |
Constructor reference | ClassName :: new For example, bySupplier<T> Return new instance |
Class arbitrary instance method reference | ClassName :: instanceMethodName For example,String::concat |
Class specific instance method references | instance:: instanceMethodName For example,this::equals |
4. On readability
Most people think Lambda expressions are difficult to read, but the pipeline structure makes them more readable. Each Lambda expression can be viewed as an execution strategy, and method references give you a clearer idea of what strategy is being executed. In addition, I often see streaming notation like the following:
new Random().ints(10)
.map(operand -> {
System.out.println("operand = " + operand);
return operand+1;
})
.forEach(System.out::println);
Copy the code
This “belly” style of writing is not recommended in functional programming. It is best to wrap a method reference separately and write it in the following style:
public void randomInt(a) {
new Random().ints(10)
.map(this::selfIncreasing)
.forEach(System.out::println);
}
/ / packaging
private int selfIncreasing(int self){
System.out.println("self = " + self);
return self+1;
}
Copy the code
Instead, it’s very readable. Pick 10 random numbers and print them out separately.
5. To summarize
Method references implement simplified representations of Lambda expressions in specific scenarios, with the goal of making code more concise. But accustomed to the traditional Java programming style of students will not adapt, I hope this article can help you solve this problem. I am: Code farmer Xiao Fat brother follow me for more programming dry goods information.
Follow our public id: Felordcn for more information
Personal blog: https://felord.cn