-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChainHashMap.java
127 lines (106 loc) · 3.41 KB
/
ChainHashMap.java
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package projectCode20280;
import java.util.ArrayList;
import java.util.HashMap;
/*
* Map implementation using hash table with separate chaining.
*/
public class ChainHashMap<K, V> extends AbstractHashMap<K, V> {
// a fixed capacity array of UnsortedTableMap that serve as buckets
private UnsortedTableMap<K, V>[] table; // initialized within createTable
/** Creates a hash table with capacity 11 and prime factor 109345121. */
public ChainHashMap() {
super();
}
/** Creates a hash table with given capacity and prime factor 109345121. */
public ChainHashMap(int cap) {
super(cap);
}
/** Creates a hash table with the given capacity and prime factor. */
public ChainHashMap(int cap, int p) {
super(cap, p);
}
/** Creates an empty table having length equal to current capacity. */
@Override
@SuppressWarnings({ "unchecked" })
protected void createTable() {
table = (UnsortedTableMap<K,V>[ ]) new UnsortedTableMap[capacity];
}
/**
* Returns value associated with key k in bucket with hash value h. If no such
* entry exists, returns null.
*
* @param h the hash value of the relevant bucket
* @param k the key of interest
* @return associate value (or null, if no such entry)
*/
@Override
protected V bucketGet(int h, K k) {
UnsortedTableMap<K,V> getter=table[h]; //returns the value from the key
if(getter==null)
return null;
return getter.get(k);
}
/**
* Associates key k with value v in bucket with hash value h, returning the
* previously associated value, if any.
*
* @param h the hash value of the relevant bucket
* @param k the key of interest
* @param v the value to be associated
* @return previous value associated with k (or null, if no such entry)
*/
@Override
protected V bucketPut(int h, K k, V v) {
UnsortedTableMap<K,V> putter=table[h];
if(putter==null)
putter=table[h]=new UnsortedTableMap<>();
int prevSize=putter.size();
V ret=putter.put(k,v);
n-=(prevSize-putter.size());//decrease size
return ret;
}
/**
* Removes entry having key k from bucket with hash value h, returning the
* previously associated value, if found.
*
* @param h the hash value of the relevant bucket
* @param k the key of interest
* @return previous value associated with k (or null, if no such entry)
*/
@Override
protected V bucketRemove(int h, K k) {
UnsortedTableMap<K,V> bucket = table[h];
if(bucket==null)
return null;
int prevSize=bucket.size();
V ret=bucket.remove(k);
n-=(prevSize-bucket.size()); //decrease size
return ret;
}
/**
* Returns an iterable collection of all key-value entries of the map.
*
* @return iterable collection of the map's entries
*/
@Override
public Iterable<Entry<K, V>> entrySet() {
ArrayList<Entry<K,V>> buffer = new ArrayList<>( );
for (int h=0; h < capacity; h++)
if (table[h] != null)
for (Entry<K,V> insertion : table[h].entrySet( )) {
buffer.add(insertion);
}
return buffer;
}
public static void main(String[] args) {
//HashMap<Integer, String> m = new HashMap<Integer, String>();
ChainHashMap<Integer, String> m = new ChainHashMap<Integer, String>();
m.put(1, "One");
m.put(10, "Ten");
m.put(11, "Eleven");
m.put(20, "Twenty");
System.out.println(m.size());
m.remove(11);
System.out.println(m.size());
}
}