|
7 | 7 | import static org.assertj.core.api.Assertions.assertThat; |
8 | 8 |
|
9 | 9 | public class HashMapInitializeTest { |
| 10 | + @Test |
| 11 | + public void initInOneLineWithFactoryMethods() { |
| 12 | + // create and initialize a HashMap from Java 9+ Map.of |
| 13 | + Map<String, Integer> map1 = new HashMap<>((Map.of("k1", 1, "k3", 2, "k2", 3))); |
| 14 | + assertThat(map1).hasSize(3); |
| 15 | + |
| 16 | + // create and initialize a HashMap from Java 9+ Map.ofEntries |
| 17 | + Map<String, Integer> map2 = new HashMap<>(Map.ofEntries(Map.entry("k4", 4), Map.entry("k5", 5))); |
| 18 | + assertThat(map2).hasSize(2); |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + public void initInOneLineFromAnExistingMap() { |
| 23 | + Map<String, Integer> map1 = new HashMap<>(Map.ofEntries(Map.entry("k4", 4), Map.entry("k5", 5))); |
| 24 | + |
| 25 | + // create and initialize from an existing map |
| 26 | + Map<String, Integer> map2 = new HashMap<>(map1); |
| 27 | + assertThat(map2).hasSize(2); |
| 28 | + } |
| 29 | + |
10 | 30 | @Test |
11 | 31 | public void initializeWithPut() { |
12 | 32 | // Create a new HashMap |
@@ -53,7 +73,7 @@ public void initializeWithPutIfAbsent() { |
53 | 73 | } |
54 | 74 |
|
55 | 75 | @Test |
56 | | - public void initializeWithPutInOneLine() { |
| 76 | + public void initializeWithDoubleBrace() { |
57 | 77 | // Create a new HashMap |
58 | 78 | Map<String, Integer> map = new HashMap<>(){{ |
59 | 79 | put("k1", 1); |
@@ -86,18 +106,5 @@ public void initializeWithPutAll() { |
86 | 106 | assertThat(map2).hasSize(5); |
87 | 107 | } |
88 | 108 |
|
89 | | - @Test |
90 | | - public void initializeFromAnotherMapWhenCreating() { |
91 | | - // create and initialize a HashMap from Java 9+ Map.of |
92 | | - Map<String, Integer> map1 = new HashMap<>((Map.of("k1", 1, "k3", 2, "k2", 3))); |
93 | | - assertThat(map1).hasSize(3); |
94 | 109 |
|
95 | | - // create and initialize a HashMap from Java 9+ Map.ofEntries |
96 | | - Map<String, Integer> map2 = new HashMap<>(Map.ofEntries(Map.entry("k4", 4), Map.entry("k5", 5))); |
97 | | - assertThat(map2).hasSize(2); |
98 | | - |
99 | | - // create and initialize from an existing map |
100 | | - Map<String, Integer> map3 = new HashMap<>(map1); |
101 | | - assertThat(map3).hasSize(3); |
102 | | - } |
103 | 110 | } |
0 commit comments