-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiMap.java
More file actions
43 lines (36 loc) · 880 Bytes
/
MultiMap.java
File metadata and controls
43 lines (36 loc) · 880 Bytes
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
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class MultiMap
{
Map<String, Set<String>> multiMap;
public MultiMap() {
multiMap = new HashMap<String, Set<String>>();
}
void add(String key, final String value) throws Exception{
if(multiMap.containsKey(key))
multiMap.get(key).add(value);
else
multiMap.put(key, new HashSet<String>(){{add(value);}});
}
HashSet<String> get(String key) throws Exception{
if(multiMap.containsKey(key)){
multiMap.get(key);
}
return null;
}
void remove(String key) throws Exception{
if(multiMap.containsKey(key)){
multiMap.remove(key);
}
}
void removeItem(String key, final String value) throws Exception{
if(multiMap.containsKey(key)){
multiMap.get(key).remove(value);
}
}
int length() throws Exception{
return multiMap.size();
}
}