Why bother remembering
int[] intArray = new int[20];
int len = intArray.length;
result = condition ? trueCase : elseCase;
https://mkyong.com/tutorials/java-8-tutorials/
Map<String, Integer> map = new HashMap<>();
map.put("A", 10);
map.put("B", 20);
// lambda
map.forEach((k, v) -> System.out.println("Key : " + k + ", Value : " + v));
more fun lambda
map.forEach(
(k, v) -> {
// yes, we can put logic here
if (k != null){
System.out.println("Key : " + k + ", Value : " + v);
}
}
);
recall Anonymous class
List<String> list = Arrays.asList("node", "java", "python", "ruby");
list.forEach(new Consumer<String>() { // anonymous class
@Override
public void accept(String str) {
System.out.println(str);
}
});
recall Lambda expressions.
List<String> list = Arrays.asList("node", "java", "python", "ruby");
list.forEach(str -> System.out.println(str)); // lambda
new cool way, Method references.
List<String> list = Arrays.asList("node", "java", "python", "ruby");
list.forEach(System.out::println); // method references
Hierarchy: Anonymous Class -> Lambda expression -> Method Reference
Note
Both lambda expression or method reference does nothing but just another way call to an existing method. With method reference, it gains better readability.
For more examples: https://mkyong.com/java8/java-8-method-references-double-colon-operator/