-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoCollectionMap.java
More file actions
43 lines (32 loc) · 1.25 KB
/
DemoCollectionMap.java
File metadata and controls
43 lines (32 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package DemoCollections;
import java.util.HashMap;
import java.util.Map;
public class DemoCollectionMap {
public static void main(String[] args) {
//Khai báo kiểu dữ liệu Map
//không lưu được 2 key trùng lặp
Map<String, String> map1 = new HashMap<>();
Map<Integer, Double> map2 = new HashMap<>();
//Thêm dữ liệu
map1.put("language", "Java");
map1.put("framework", "TestNG");
map1.put("library", "Selenium");
map2.put(1, 111D);
map2.put(2, 222D);
System.out.println("Map 1: " + map1);
System.out.println("Map 2: " + map2);
//Update value
map1.put("library", "Playwright");
System.out.println(map1.containsKey("language123"));
System.out.println(map1.containsValue("Java123"));
//map1.remove("library");
//Duyệt kiểu dữ liệu Map
//Get hết giá trị của key và value
for (Map.Entry<String, String> entry : map1.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
for (Map.Entry<Integer, Double> entry : map2.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}