I am currently reading the book 'Java 8 in Action: Lambdas, streams, and functional-style programming' by Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft and this question is about an example which introduces to Lambda expressions in chapter 2.
Based on the text book example, I've written these Java classes.
It appears that Java 8 lambda expressions are performing worse than pre-java8 code written using anonymous inner classes.
- Java 8 lambda - 51145201 nanos
- Plain java - 1859449 nanos
I would like to hear your opinions, have I understood something wrong or is my test incorrect? At this moment, I am not getting motivated to proceed further with learning Java 8.
To try the examples, please copy these classes (Apple.java, Formatter.java, AppWithLambda.java and AppWithoutLambda.java) under package 'com.prash.parametarisedbehaviour' into your IDE and execute both AppX main classes.
package com.prash.parametarisedbehaviour;
public class Apple {
private String colour;
private Integer weight;
public Apple(String colour, int weight) {
super();
this.colour = colour;
this.weight = weight;
}
public String getColour() {
return colour;
}
public void setColour(String colour) {
this.colour = colour;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
}
package com.prash.parametarisedbehaviour;
public interface Formatter<T> {
String accept(T t);
}
package com.prash.parametarisedbehaviour;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class AppWithoutLambda {
public static void main(String[] args) throws IOException {
runWithoutLambda();
}
@SuppressWarnings("unchecked")
public static <T> void runWithoutLambda() throws IOException {
long start = System.nanoTime();
List<T> inventory = (List<T>) Arrays.asList(new Apple("red", 100), new Apple("green", 160),
new Apple("yellow", 170), new Apple("blue", 130));
System.out.println("Applying weight formatter...");
prettyPrint(inventory, new Formatter<T>() {
public String accept(T a) {
String characteristic = ((Apple) a).getWeight() > 150 ? "heavy" : "light";
return "A " + characteristic + " apple.";
}
});
System.out.println("Applying colour formatter...");
prettyPrint(inventory, new Formatter<T>() {
public String accept(T t) {
return "A " + ((Apple) t).getColour() + " apple.";
}
});
long end = System.nanoTime() - start;
System.out.println("Time taken without lambda: " + end + " nanos");
}
public static <T> void prettyPrint(List<T> inventory, Formatter<T> formatter) {
for (T t : inventory) {
String output = formatter.accept(t);
System.out.println(output);
}
}
}
package com.prash.parametarisedbehaviour;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class AppWithLambda {
public static void main(String[] args) throws IOException {
runWithLamda();
}
@SuppressWarnings("unchecked")
public static <T> void runWithLamda() throws IOException {
long start = System.nanoTime();
List<T> inventory = (List<T>) Arrays.asList(new Apple("red", 100), new Apple("green", 160),
new Apple("yellow", 170), new Apple("blue", 130));
System.out.println("Applying weight formatter...");
prettyPrintWithStream(inventory, (T a) -> {
String characteristic = ((Apple) a).getWeight() > 150 ? "heavy" : "light";
return "A " + characteristic + " apple.";
});
System.out.println("Applying colour formatter...");
prettyPrintWithStream(inventory, (T a) -> "A " + ((Apple) a).getColour() + " apple.");
long end = System.nanoTime() - start;
System.out.println("Time taken with Lambda: " + end + " nanos");
}
public static <T> void prettyPrintWithStream(List<T> inventory, Formatter<T> formatter) {
inventory.stream().forEach((t) -> {
String output = formatter.accept(t);
System.out.println(output);
});
}
}