-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodReferenceExample.java
More file actions
35 lines (27 loc) · 1.2 KB
/
Copy pathMethodReferenceExample.java
File metadata and controls
35 lines (27 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* Day 34 - Java 8 Features: Method References
*/
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class MethodReferenceExample {
public static void main(String[] args) {
System.out.println("=== Method References ===\n");
List<String> names = Arrays.asList("alice", "Bob", "charlie");
System.out.println("--- forEach(System.out::println) ---");
names.forEach(System.out::println);
System.out.println("\n--- map(String::toUpperCase) ---");
List<String> upper = names.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println(upper);
System.out.println("\n--- sorted(String::compareToIgnoreCase) ---");
List<String> sorted = names.stream()
.sorted(String::compareToIgnoreCase)
.collect(Collectors.toList());
System.out.println(sorted);
System.out.println("\nKey Points:");
System.out.println("1. Method refs are shorthand for lambdas that call an existing method");
System.out.println("2. Common forms: Class::staticMethod, obj::instanceMethod, Class::instanceMethod");
}
}