I will write an excerpt of what I found interesting while reading Effective Java 3rd Edition. The book itself is still being read, so it may be added in the future. In that case, it is undecided whether to update this article or write a new article.
Item 42 : Prefer lambdas to anonymous classes The writing method of having a unique method implementation for each enum instance, which was introduced in "Item 34: Mimicking an extensible enum with an interface" in Effective Java 2nd edition, has been changed to one using lambda. Click here for how to write in the 2nd edition.
EnumFunctionV2.java
public enum EnumFunctionV2 {
PLUS("+") {
@Override
public double apply(double x, double y){ return x + y; }
},
MINUS("-") {
@Override
public double apply(double x, double y){ return x - y; }
},
TIMES("*") {
@Override
public double apply(double x, double y){ return x * y; }
},
DEVIDE("/") {
@Override
public double apply(double x, double y){ return x / y; }
};
private final String symbol;
//constructor
EnumFunctionV2(String symbol) {
this.symbol = symbol;
}
@Override
public String toString() {
return symbol;
}
public abstract double apply(double x, double y);
}
It is a way to define an abstract method and override it on each enum instance. It's convenient, but it's a little hard to see ... Click here for how to write in the 3rd edition.
EnumFunctionV3.java
import java.util.function.DoubleBinaryOperator;
public enum EnumFunctionV3 {
PLUS("+", (x, y) -> x + y),
MINUS("-", (x, y) -> x - y),
TIMES("*", (x, y) -> x * y),
DEVIDE("/", (x, y) -> x / y);
private final String symbol;
private final DoubleBinaryOperator op;
//constructor
EnumFunctionV3(String symbol, DoubleBinaryOperator op) {
this.symbol = symbol;
this.op = op;
}
@Override
public String toString() {
return symbol;
}
public double apply(double x, double y) {
return op.applyAsDouble(x, y);
}
}
abstract has disappeared. Have DoubleBinaryOperator in the second argument of the constructor, and use lambda to make the method I'm writing the implementation directly. The content is the same as in the second edition, but I feel that this is cleaner and easier to see. Both are called in the same way.
EnumFunctionTest.java
public class EnumFunctionTest {
public static void main(String... args) {
//Call the 2nd edition
System.out.println("【v2】");
//Method invocation of all instances while looping
for (EnumFunctionV2 func2: EnumFunctionV2.values()) {
System.out.print(func2 + ":");
System.out.println(func2.apply(1, 2));
}
//If you want to call the method of each instance individually, it looks like this
System.out.println(EnumFunctionV2.PLUS.apply(1, 2));
//Call the 3rd edition
System.out.println("【v3】");
//Method invocation of all instances while looping
for (EnumFunctionV3 func3: EnumFunctionV3.values()) {
System.out.print(func3 + ":");
System.out.println(func3.apply(1, 2));
}
//If you want to call the method of each instance individually, it looks like this
System.out.println(EnumFunctionV3.PLUS.apply(1, 2));
}
}
Execution result 【v2】 +:3.0 -:-1.0 *:2.0 /:0.5 3.0 【v3】 +:3.0 -:-1.0 *:2.0 /:0.5 3.0
that's all. I was surprised at how to use lambda, so I wrote it down.
As I wrote at the beginning, I'm currently reading Effective Java 3rd Edition (May 2, 2019). Moreover, since I haven't read it from the beginning, I wrote about Item 42 this time (May 2, 2019), but there is a possibility that I will add about Item 1 after this. The third edition has Items 1 to 90. Effective Java is a book that has been treated as a masterpiece of Java for a long time, so I think that Java users should read it once. The third edition was released on January 6, 2018 for the English version and October 30, 2018 for the Japanese version. I think it can be used for a while, so if you are interested, please read it.
Recommended Posts