Skip to content

Commit a98c3ac

Browse files
committed
map
1 parent d15a266 commit a98c3ac

4 files changed

Lines changed: 150 additions & 0 deletions

File tree

‎java-basic/pom.xml‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
6+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
7+
<modelVersion>4.0.0</modelVersion>
8+
<groupId>com.mkyong</groupId>
9+
<artifactId>io</artifactId>
10+
<version>1.0</version>
11+
12+
<name>java-basic</name>
13+
<url>https://mkyong.com</url>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<maven.compiler.source>11</maven.compiler.source>
18+
<maven.compiler.target>11</maven.compiler.target>
19+
<java.version>11</java.version>
20+
<junit.version>5.4.0</junit.version>
21+
</properties>
22+
23+
<dependencies>
24+
25+
<dependency>
26+
<groupId>org.junit.jupiter</groupId>
27+
<artifactId>junit-jupiter-params</artifactId>
28+
<version>${junit.version}</version>
29+
<scope>test</scope>
30+
</dependency>
31+
32+
</dependencies>
33+
34+
<build>
35+
<finalName>java-basic</finalName>
36+
<plugins>
37+
<plugin>
38+
<groupId>org.apache.maven.plugins</groupId>
39+
<artifactId>maven-compiler-plugin</artifactId>
40+
<version>3.8.1</version>
41+
<configuration>
42+
<source>${java.version}</source>
43+
<target>${java.version}</target>
44+
</configuration>
45+
</plugin>
46+
</plugins>
47+
</build>
48+
</project>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.mkyong.collections.map;
2+
3+
import java.util.Collections;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import static java.util.stream.Collectors.toMap;
7+
8+
public class InitMap {
9+
10+
public static Map<String, String> map5;
11+
12+
static {
13+
// mutable Map
14+
map5 = new HashMap<>();
15+
map5.put("key1", "value1");
16+
map5.put("key2", "value2");
17+
map5.put("key3", "value3");
18+
}
19+
20+
public static void main(String[] args) {
21+
22+
// normal way, mutable Map
23+
Map<String, String> map = new HashMap<>();
24+
map.put("key1", "value1");
25+
map.put("key2", "value2");
26+
map.put("key3", "value3");
27+
System.out.println(map.getClass()); // java.util.HashMap
28+
29+
// immutable Map
30+
Map<String, String> immutableMap = Collections.unmodifiableMap(map);
31+
System.out.println(immutableMap.getClass()); // java.util.Collections$UnmodifiableMap
32+
//immutableMap.put("key4", "value4"); // UnsupportedOperationException
33+
34+
// @since 1.3
35+
// single entry immutable Map
36+
Map<String, String> map1 = Collections.singletonMap("key1", "value1");
37+
System.out.println(map1.getClass()); // java.util.Collections$SingletonMap
38+
//map1.put("key2", "value2"); // throws UnsupportedOperationException
39+
40+
// Java 9, max is 10 entries or elements, immutable map
41+
Map<String, String> map2 = Map.of(
42+
"key1", "value1",
43+
"key2", "value2",
44+
"key3", "value3"
45+
);
46+
System.out.println(map2.getClass()); // java.util.ImmutableCollections$MapN
47+
//map2.put("key3", "value4"); // throws UnsupportedOperationException
48+
49+
// Java 9, no limit of Map entry, immutable Map.
50+
Map<String, String> map3 = Map.ofEntries(
51+
Map.entry("key1", "value1"),
52+
Map.entry("key2", "value2"),
53+
Map.entry("key3", "value3")
54+
);
55+
System.out.println(map3.getClass()); // java.util.ImmutableCollections$MapN
56+
//map3.put("key3", "value4"); // throws UnsupportedOperationException
57+
58+
// It works for all Java version, mutable Map.
59+
Map<String, String> map4 = createMap();
60+
System.out.println(map4.getClass()); // java.util.HashMap
61+
map4.put("key4", "value4"); // no problem.
62+
63+
// static init
64+
System.out.println(map5.getClass()); // java.util.HashMap
65+
map5.put("key4", "value4"); // no problem.*/
66+
67+
// Java 8, mutable Map, do not use this, just for reference
68+
/*Map<String, String> map6 = Stream.of(
69+
new AbstractMap.SimpleEntry<>("key1", "value1"),
70+
new AbstractMap.SimpleEntry<>("key2", "value2"),
71+
new AbstractMap.SimpleEntry<>("key3", "value3"))
72+
.collect(
73+
toMap(AbstractMap.SimpleEntry::getKey,
74+
AbstractMap.SimpleEntry::getValue)
75+
);
76+
System.out.println(map6.getClass()); // java.util.HashMap
77+
map6.put("key4", "value4"); // no problem.*/
78+
79+
// classic way, loop a Map
80+
for (Map.Entry<String, String> entry : map.entrySet()) {
81+
System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
82+
}
83+
84+
}
85+
86+
private static Map<String, String> createMap() {
87+
Map<String, String> map = new HashMap<>();
88+
map.put("key1", "value1");
89+
map.put("key2", "value2");
90+
map.put("key3", "value3");
91+
return map;
92+
}
93+
94+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
datasource.url=jdbc:mysql://localhost/test?useSSL=false
2+
datasource.username=test
3+
datasource.password=password
4+
datasource.driver-class-name=com.mysql.jdbc.Driver
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "unit test",
3+
"age": 38
4+
}

0 commit comments

Comments
 (0)