-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashtableWithDuplicateKeysAE.java
More file actions
104 lines (90 loc) · 3.05 KB
/
HashtableWithDuplicateKeysAE.java
File metadata and controls
104 lines (90 loc) · 3.05 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import java.util.List;
import java.util.NoSuchElementException;
import java.util.ArrayList;
/**
* This class allows multiple values to be associated with the same key by
* grouping such duplicate values together in a list.
*
* This class builds on top of the P1W2 HashtableMap implementation.
*
* @author AlgorithmEngineer
*/
public class HashtableWithDuplicateKeysAE<KeyType, ValueType> extends HashtableMap<KeyType, List<ValueType>>
implements HashtableWithDuplicateKeysInterface<KeyType, ValueType> {
int numberOfValues;
// pass through constructors:
public HashtableWithDuplicateKeysAE(int capacity) {
super(capacity);
numberOfValues = 0;
}
public HashtableWithDuplicateKeysAE() {
super();
numberOfValues = 0;
}
/**
* This putOne method adds a single value to the list of values associated with
* the specified key. This is different from the inherited put method, which
* assigns an entire List<ValueType> to the specified key.
*
* @param key is used to store and look up this mapping
* @param value is one more value that should be associated with given key
*/
public void putOne(KeyType key, ValueType value) {
// if the key is already a part of this collection
if (containsKey(key)) {
List<ValueType> values = get(key);
// and it doesn't already map to the specified value
if (!values.contains(value))
// then add this value to the list of values for the given key
values.add(value);
} else {
// otherwise map this key to a new list containing this value
List<ValueType> values = new ArrayList<>();
values.add(value);
put(key, values);
}
numberOfValues++; // and track the number of values added via this method
}
/**
* This removeOne method removes a single value from the list of values
* associated with the specified key. This is different from the inherited
* remove method, which removes the entire list of values mapped to from this
* key.
*
* @param key is the key that this value is being removed from
* @param value is the specific value that should be removed
*/
public void removeOne(KeyType key, ValueType value) {
List<ValueType> values = get(key);
values.remove(value);
if (values.size() == 0)
remove(key);
numberOfValues--; // and track the number of values removed via this method
}
/**
* @return int number of values stored in this collection
*/
public int getNumberOfValues() {
return numberOfValues;
}
// the following overrides are meant to help keep numberOfValues in sync
@Override
public void clear() {
numberOfValues = 0;
super.clear();
}
@Override
public List<ValueType> remove(KeyType key) {
numberOfValues -= this.get(key).size();
return super.remove(key);
}
@Override
public void put(KeyType key, List<ValueType> values) {
try {
numberOfValues -= this.get(key).size();
} catch (NoSuchElementException e) {
} // nothing to remove
numberOfValues += values.size();
super.put(key, values);
}
}