-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMulHashMap.java
More file actions
40 lines (37 loc) · 1.23 KB
/
Copy pathMulHashMap.java
File metadata and controls
40 lines (37 loc) · 1.23 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
import java.util.*;
public class MulHashMap{
public static void main(String[] args){
HashMap<Integer, String> hm = new HashMap<>();
hm.put(1, "aaa");
hm.put(2, "bbb");
hm.put(3, "ccc");
hm.put(1, "aaa111");
hm.put(1, "aaa222");
//ArrayList<String> valueArr = hm.get(1);
System.out.println(hm.get(2));
System.out.println(hm.get(1));
System.out.println("-------");
HashMap<Integer, List<String>> ht = new HashMap<>();
store(ht,1, "aaa0");
store(ht,1, "aaa1");
store(ht,1, "aaa2");
store(ht,2, "bbb");
store(ht,2, "bbbb");
store(ht,3, "ccc");
store(ht,4, "ddd");
System.out.println(ht.get(1));
System.out.println(ht.get(3));
}
public static void store(HashMap<Integer, List<String>> hm, int key, String str){
if(hm.containsKey(key)){
hm.get(key).add(str);
}else{
ArrayList<String> arr = new ArrayList<>();
arr.add(str);
hm.put(key, arr);
}
}
public static List<String> getLists(HashMap<Integer, List<String>> hm, int key){
return hm.get(key);
}
}