-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodRef4.java
More file actions
41 lines (35 loc) · 1.43 KB
/
Copy pathMethodRef4.java
File metadata and controls
41 lines (35 loc) · 1.43 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
35
36
37
38
39
40
41
package methodref;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
public class MethodRef4 {
public static void main(String[] args) {
List<Person> personList = List.of(new Person("Kim"),
new Person("Park"),
new Person("Lee")
);
List<String> result1 = mapPersonToString(personList, (Person p) -> p.introduce());
List<String> result2 = mapPersonToString(personList, Person::introduce);
System.out.println("result1 = " + result1);
System.out.println("result2 = " + result2);
List<String> stringResult1 = mapStringToString(result1, (String s)-> s.toUpperCase());
List<String> stringResult2 = mapStringToString(result1, String::toUpperCase);
System.out.println("stringResult1 = " + stringResult1);
}
public static List<String> mapPersonToString(List<Person> personList, Function<Person, String> fun){
List<String> result = new ArrayList<>();
for (Person p : personList) {
String apply = fun.apply(p);
result.add(apply);
}
return result;
}
public static List<String> mapStringToString(List<String> stringList, Function<String, String> fun){
List<String> result = new ArrayList<>();
for (String p : stringList) {
String apply = fun.apply(p);
result.add(apply);
}
return result;
}
}