|
5 | 5 | import lombok.extern.slf4j.Slf4j; |
6 | 6 | import org.junit.Test; |
7 | 7 |
|
| 8 | +import java.util.HashMap; |
8 | 9 | import java.util.List; |
9 | 10 | import java.util.Map; |
10 | 11 | import java.util.stream.Collectors; |
|
17 | 18 | public class ConvertListToMapTest { |
18 | 19 |
|
19 | 20 | @Test |
20 | | - public void convert_students_to_map1() { |
| 21 | + public void convert_students_to_map_of_nameVsAge_beforeJava8() { |
21 | 22 | int max = 3; |
22 | 23 | List<Student> students = TestUtil.getStudentSample(max); |
| 24 | + Map<String, Integer> nameVsAgeMap = new HashMap<>(); |
| 25 | + Student student; |
| 26 | + for (int i = 0; i < students.size(); i++) { |
| 27 | + student = students.get(i); |
| 28 | + nameVsAgeMap.put(student.getName(), student.getAge()); |
| 29 | + } |
23 | 30 |
|
24 | | - Map<String, Integer> nameVsAgeMap1 = students |
| 31 | + assertThat(nameVsAgeMap.size()).isEqualTo(max); |
| 32 | + log.info("nameVsAgeMap : {}", nameVsAgeMap); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void convert_students_to_map_of_nameVsAge() { |
| 37 | + int max = 3; |
| 38 | + List<Student> students = TestUtil.getStudentSample(max); |
| 39 | + |
| 40 | + Map<String, Integer> nameVsAgeMap = students |
25 | 41 | .stream() |
26 | 42 | .collect(Collectors.toMap( |
27 | 43 | i1 -> i1.getName(), |
28 | 44 | i2 -> i2.getAge()) |
29 | 45 | ); |
30 | 46 |
|
31 | | - assertThat(nameVsAgeMap1.size()).isEqualTo(max); |
32 | | - log.info("nameVsAgeMap1 : {}", nameVsAgeMap1); |
| 47 | + assertThat(nameVsAgeMap.size()).isEqualTo(max); |
| 48 | + log.info("nameVsAgeMap : {}", nameVsAgeMap); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void convert_students_to_map_of_nameVsAge_method_reference() { |
| 53 | + int max = 3; |
| 54 | + List<Student> students = TestUtil.getStudentSample(max); |
33 | 55 |
|
34 | 56 | //method reference |
35 | | - Map<String, Integer> nameVsAgeMap2 = students |
| 57 | + Map<String, Integer> nameVsAgeMap = students |
36 | 58 | .stream() |
37 | 59 | .collect(Collectors.toMap( |
38 | 60 | Student::getName, |
39 | 61 | Student::getAge) |
40 | 62 | ); |
41 | 63 |
|
42 | | - assertThat(nameVsAgeMap2.size()).isEqualTo(max); |
43 | | - |
| 64 | + assertThat(nameVsAgeMap.size()).isEqualTo(max); |
44 | 65 | } |
45 | 66 |
|
46 | 67 | @Test |
47 | | - public void convert_students_to_map2() { |
| 68 | + public void convert_students_to_map_of_idVsStudent() { |
48 | 69 | int max = 3; |
49 | 70 | List<Student> students = TestUtil.getStudentSample(max); |
50 | 71 |
|
51 | | - Map<Integer, Student> nameVsAgeMap1 = IntStream.range(0, max).boxed() |
| 72 | + Map<Integer, Student> idVsStudentMap = IntStream.range(0, max).boxed() |
52 | 73 | .collect(Collectors.toMap( |
53 | 74 | i1 -> i1 + 1, |
54 | 75 | i2 -> students.get(i2) |
55 | 76 | )); |
56 | 77 |
|
57 | | - nameVsAgeMap1.forEach((it, it2) -> log.info("{}", it)); |
| 78 | + idVsStudentMap.forEach((it, it2) -> log.info("{}", it)); |
58 | 79 |
|
59 | | - assertThat(nameVsAgeMap1.size()).isEqualTo(max); |
| 80 | + assertThat(idVsStudentMap.size()).isEqualTo(max); |
60 | 81 |
|
61 | 82 | } |
62 | 83 |
|
|
0 commit comments