-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathMultiset.java
More file actions
47 lines (38 loc) · 849 Bytes
/
Multiset.java
File metadata and controls
47 lines (38 loc) · 849 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
44
45
46
47
import java.util.HashMap;
/* @author: jaswant developer.jaswant@gmail.com
* @algorithm: hashing
* @use: holding frequency map, similar to multiset in c++
*/
class MultiSet<K> {
private HashMap<K, Integer> multiSet = new HashMap<K, Integer>();
private int size;
public int get(K key){
return multiSet.getOrDefault(key, 0);
}
public void add(K key){
size++;
multiSet.put(key, get(key)+ 1);
}
public void remove(K key){
int freq = get(key);
size--;
if(freq == 1){
multiSet.remove(key);
}else{
multiSet.put(key, freq - 1);
}
}
public int size(){
return size;
}
public boolean isEmpty(){
return size == 0;
}
public boolean containsKey(K key){
return multiSet.containsKey(key);
}
@Override
public String toString(){
return multiSet.toString();
}
}