Skip to content

Commit 7ff4d2e

Browse files
dhruba619maibin
authored andcommitted
BAEL-1983 Intialize a HashMap in Java (eugenp#4819)
1 parent 33889d0 commit 7ff4d2e

3 files changed

Lines changed: 140 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.java9.maps.initialize;
2+
3+
import java.util.AbstractMap;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
public class MapsInitializer {
8+
9+
@SuppressWarnings("unused")
10+
public void createMapWithMapOf() {
11+
Map<String, String> emptyMap = Map.of();
12+
Map<String, String> singletonMap = Map.of("key1", "value");
13+
Map<String, String> map = Map.of("key1","value1", "key2", "value2");
14+
}
15+
16+
public void createMapWithMapEntries() {
17+
Map<String, String> map = Map.ofEntries(
18+
new AbstractMap.SimpleEntry<String, String>("name", "John"),
19+
new AbstractMap.SimpleEntry<String, String>("city", "budapest"),
20+
new AbstractMap.SimpleEntry<String, String>("zip", "000000"),
21+
new AbstractMap.SimpleEntry<String, String>("home", "1231231231")
22+
);
23+
}
24+
25+
@SuppressWarnings("unused")
26+
public void createMutableMaps() {
27+
Map<String, String> map = new HashMap<String, String> (Map.of("key1","value1", "key2", "value2"));
28+
Map<String, String> map2 = new HashMap<String, String> ( Map.ofEntries(
29+
new AbstractMap.SimpleEntry<String, String>("name", "John"),
30+
new AbstractMap.SimpleEntry<String, String>("city", "budapest")));
31+
32+
}
33+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.baeldung.java.map.initialize;
2+
3+
import java.util.AbstractMap;
4+
import java.util.Collections;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import java.util.stream.Collectors;
8+
import java.util.stream.Stream;
9+
10+
public class MapInitializer {
11+
12+
public static Map<String, String> articleMapOne;
13+
static {
14+
articleMapOne = new HashMap<>();
15+
articleMapOne.put("ar01", "Intro to Map");
16+
articleMapOne.put("ar02", "Some article");
17+
}
18+
19+
public static Map<String, String> createSingletonMap() {
20+
Map<String, String> passwordMap = Collections.singletonMap("username1", "password1");
21+
return passwordMap;
22+
23+
}
24+
25+
public Map<String, String> createEmptyMap() {
26+
Map<String, String> emptyMap = Collections.emptyMap();
27+
return emptyMap;
28+
}
29+
30+
public Map<String, String> createUsingDoubleBrace() {
31+
Map<String, String> doubleBraceMap = new HashMap<String, String>() {
32+
33+
/**
34+
*
35+
*/
36+
private static final long serialVersionUID = 1L;
37+
38+
{
39+
put("key1", "value1");
40+
put("key2", "value2");
41+
}
42+
};
43+
return doubleBraceMap;
44+
}
45+
46+
public Map<String, String> createMapUsingStreamStringArray() {
47+
Map<String, String> map = Stream.of(new String[][] { { "Hello", "World" }, { "John", "Doe" }, })
48+
.collect(Collectors.toMap(data -> data[0], data -> data[1]));
49+
50+
return map;
51+
}
52+
53+
public Map<String, Integer> createMapUsingStreamObjectArray() {
54+
Map<String, Integer> map = Stream.of(new Object[][] { { "data1", 1 }, { "data2", 2 }, })
55+
.collect(Collectors.toMap(data -> (String) data[0], data -> (Integer) data[1]));
56+
57+
return map;
58+
}
59+
60+
public Map<String, Integer> createMapUsingStreamSimpleEntry() {
61+
Map<String, Integer> map = Stream.of(new AbstractMap.SimpleEntry<>("idea", 1), new AbstractMap.SimpleEntry<>("mobile", 2))
62+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
63+
64+
return map;
65+
}
66+
67+
public Map<String, Integer> createMapUsingStreamSimpleImmutableEntry() {
68+
Map<String, Integer> map = Stream.of(new AbstractMap.SimpleImmutableEntry<>("idea", 1), new AbstractMap.SimpleImmutableEntry<>("mobile", 2))
69+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
70+
71+
return map;
72+
}
73+
74+
public Map<String, String> createImmutableMapWithStreams() {
75+
Map<String, String> map = Stream.of(new String[][] { { "Hello", "World" }, { "John", "Doe" }, })
76+
.collect(Collectors.collectingAndThen(Collectors.toMap(data -> data[0], data -> data[1]), Collections::<String, String> unmodifiableMap));
77+
return map;
78+
79+
}
80+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.java.map.initialize;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.Map;
6+
7+
import org.junit.Test;
8+
9+
public class MapInitializerUnitTest {
10+
11+
@Test
12+
public void givenStaticMap_whenUpdated_thenCorrect() {
13+
14+
MapInitializer.articleMapOne.put("NewArticle1", "Convert array to List");
15+
16+
assertEquals(MapInitializer.articleMapOne.get("NewArticle1"), "Convert array to List");
17+
18+
}
19+
20+
@Test(expected=UnsupportedOperationException.class)
21+
public void givenSingleTonMap_whenEntriesAdded_throwsException() {
22+
23+
Map<String, String> map = MapInitializer.createSingletonMap();
24+
map.put("username2", "password2");
25+
}
26+
27+
}

0 commit comments

Comments
 (0)