|
17 | 17 | public class ConvertListToMapTest { |
18 | 18 |
|
19 | 19 | @Test |
20 | | - public void convert_students_to_map1() { |
| 20 | + public void convert_students_to_map_of_nameVsAge() { |
21 | 21 | int max = 3; |
22 | 22 | List<Student> students = TestUtil.getStudentSample(max); |
23 | 23 |
|
24 | | - Map<String, Integer> nameVsAgeMap1 = students |
| 24 | + Map<String, Integer> nameVsAgeMap = students |
25 | 25 | .stream() |
26 | 26 | .collect(Collectors.toMap( |
27 | 27 | i1 -> i1.getName(), |
28 | 28 | i2 -> i2.getAge()) |
29 | 29 | ); |
30 | 30 |
|
31 | | - assertThat(nameVsAgeMap1.size()).isEqualTo(max); |
32 | | - log.info("nameVsAgeMap1 : {}", nameVsAgeMap1); |
| 31 | + assertThat(nameVsAgeMap.size()).isEqualTo(max); |
| 32 | + log.info("nameVsAgeMap : {}", nameVsAgeMap); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void convert_students_to_map_of_nameVsAge_method_reference() { |
| 37 | + int max = 3; |
| 38 | + List<Student> students = TestUtil.getStudentSample(max); |
33 | 39 |
|
34 | 40 | //method reference |
35 | | - Map<String, Integer> nameVsAgeMap2 = students |
| 41 | + Map<String, Integer> nameVsAgeMap = students |
36 | 42 | .stream() |
37 | 43 | .collect(Collectors.toMap( |
38 | 44 | Student::getName, |
39 | 45 | Student::getAge) |
40 | 46 | ); |
41 | 47 |
|
42 | | - assertThat(nameVsAgeMap2.size()).isEqualTo(max); |
43 | | - |
| 48 | + assertThat(nameVsAgeMap.size()).isEqualTo(max); |
44 | 49 | } |
45 | 50 |
|
46 | 51 | @Test |
47 | | - public void convert_students_to_map2() { |
| 52 | + public void convert_students_to_map_of_idVsStudent() { |
48 | 53 | int max = 3; |
49 | 54 | List<Student> students = TestUtil.getStudentSample(max); |
50 | 55 |
|
51 | | - Map<Integer, Student> nameVsAgeMap1 = IntStream.range(0, max).boxed() |
| 56 | + Map<Integer, Student> idVsStudentMap = IntStream.range(0, max).boxed() |
52 | 57 | .collect(Collectors.toMap( |
53 | 58 | i1 -> i1 + 1, |
54 | 59 | i2 -> students.get(i2) |
55 | 60 | )); |
56 | 61 |
|
57 | | - nameVsAgeMap1.forEach((it, it2) -> log.info("{}", it)); |
| 62 | + idVsStudentMap.forEach((it, it2) -> log.info("{}", it)); |
58 | 63 |
|
59 | | - assertThat(nameVsAgeMap1.size()).isEqualTo(max); |
| 64 | + assertThat(idVsStudentMap.size()).isEqualTo(max); |
60 | 65 |
|
61 | 66 | } |
62 | 67 |
|
|
0 commit comments