-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChainHashMap.java
169 lines (153 loc) · 4.8 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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> bucket = table[h];
return bucket == null ? null : bucket.get(k);
}
@Override
public V get(K k)
{
for (int i=0; i<table.length; i++)
{
if(table[i]!=null)
{
if(bucketGet(i, k)!=null) {
return bucketGet(i, k);
}
}
}
return null;
}
/**
* 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> bucket = table[h];
if(bucket == null)
{
bucket = new UnsortedTableMap<K, V>();
table[h]=bucket;
}
int prev = bucket.size();
V old = bucket.put(k,v);
n+= (bucket.size() - prev);
return old;
}
/**
* 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 prev = bucket.size();
V old = bucket.remove(k);
n -= ( prev - bucket.size());
return old;
}
public V remove(K k)
{
for (int i=0; i<table.length; i++)
{
if(table[i]!=null)
{
UnsortedTableMap<K, V> bucket = table[i];
if(bucketGet(i, k)!=null) {
return bucketRemove(i, k);
}
}
}
return null;
}
/**
* 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>> it = new ArrayList<Entry<K,V>>();
for (int i=0;i<capacity;i++)
{
UnsortedTableMap<K,V> bucket = table[i];
if(bucket!=null)
{
for (Entry<K,V> entry : bucket.entrySet()) {
it.add(entry);
}
}
}
return it;
}
public String toString(){
String s = "< ";
for(int i=0;i<capacity; i++)
{
UnsortedTableMap<K,V> bucket = table[i];
if(bucket!=null)
{
s+=bucket.toString()+" ";
}
}
return s+=">";
}
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: " + m);
m.remove(11);
System.out.println("m: " + m);
}
}