|
5 | 5 | * @use: holding frequency map, similar to multiset in c++
|
6 | 6 | */
|
7 | 7 | class MultiSet<K> {
|
8 | | -private HashMap<K, Integer> multiSet = new HashMap<K, Integer>(); |
| 8 | +private HashMap<K, Integer> multiSet = new HashMap<K, Integer>(); |
9 | 9 |
|
10 | | -int get(K key){ |
11 | | -return multiSet.getOrDefault(key, 0); |
12 | | -} |
| 10 | +int get(K key){ |
| 11 | +return multiSet.getOrDefault(key, 0); |
| 12 | +} |
13 | 13 |
|
14 | | -void add(K key){ |
15 | | -multiSet.put(key, get(key)+ 1); |
16 | | -} |
| 14 | +void add(K key){ |
| 15 | +multiSet.put(key, get(key)+ 1); |
| 16 | +} |
17 | 17 |
|
18 | | -void remove(K key){ |
19 | | -int freq = get(key); |
20 | | -if(freq == 1){ |
21 | | -multiSet.remove(key); |
22 | | -}else{ |
23 | | -multiSet.put(key, freq - 1); |
24 | | -} |
25 | | -} |
| 18 | +void remove(K key){ |
| 19 | +int freq = get(key); |
| 20 | +if(freq == 1){ |
| 21 | +multiSet.remove(key); |
| 22 | +}else{ |
| 23 | +multiSet.put(key, freq - 1); |
| 24 | +} |
| 25 | +} |
26 | 26 |
|
27 | | -@Override |
28 | | -public String toString(){ |
29 | | -return multiSet.toString(); |
30 | | -} |
| 27 | +@Override |
| 28 | +public String toString(){ |
| 29 | +return multiSet.toString(); |
| 30 | +} |
31 | 31 | }
|
0 commit comments