-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnsortedTableMap.java
161 lines (134 loc) · 3.65 KB
/
UnsortedTableMap.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
package projectCode20280;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* An implementation of a map using an unsorted table.
*/
public class UnsortedTableMap<K, V> extends AbstractMap<K, V> {
/** Underlying storage for the map of entries. */
private ArrayList<MapEntry<K, V>> table = new ArrayList<>();
/** Constructs an initially empty map. */
public UnsortedTableMap() {
}
// private utility
/** Returns the index of an entry with equal key, or -1 if none found. */
private int findIndex(K key) {
int n = table.size();
//simple linear search to find index
for (int j = 0; j < n; j++)
{
if (table.get(j).getKey().equals(key))
{
return j;
}
}
return -1;
}
// public methods
/**
* Returns the number of entries in the map.
*
* @return number of entries in the map
*/
@Override
public int size() {
return table.size();
}
/**
* Returns the value associated with the specified key, or null if no such entry
* exists.
*
* @param key the key whose associated value is to be returned
* @return the associated value, or null if no such entry exists
*/
@Override
public V get(K key) {
//find index and return value
int i = findIndex(key);
if (i == -1)
{
return null;
}
return table.get(i).getValue();
}
/**
* Associates the given value with the given key. If an entry with the key was
* already in the map, this replaced the previous value with the new one and
* returns the old value. Otherwise, a new entry is added and null is returned.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return the previous value associated with the key (or null, if no such
* entry)
*/
@Override
public V put(K key, V value) {
int i = findIndex(key);
if (i == -1)
{
table.add(new MapEntry<>(key, value));
return null;
}
return table.get(i).setValue(value);
}
/**
* Removes the entry with the specified key, if present, and returns its value.
* Otherwise does nothing and returns null.
*
* @param key the key whose entry is to be removed from the map
* @return the previous value associated with the removed key, or null if no
* such entry exists
*/
@Override
public V remove(K key) {
int i = findIndex(key);
int n = size();
if (i == -1)
{
return null;
}
V temp = table.get(i).getValue();
//reorder so to not leave any gaps
if (i != (n - 1))
{
table.set(i, table.get(n-1));
}
table.remove(n-1);
return temp;
}
// ---------------- nested EntryIterator class ----------------
private class EntryIterator implements Iterator<Entry<K, V>> {
private int j = 0;
public boolean hasNext() {
return j < table.size();
}
public Entry<K, V> next() {
if (j == table.size())
throw new NoSuchElementException("No further entries");
return table.get(j++);
}
public void remove() {
throw new UnsupportedOperationException("remove not supported");
}
} // ----------- end of nested EntryIterator class -----------
// ---------------- nested EntryIterable class ----------------
private class EntryIterable implements Iterable<Entry<K, V>> {
public Iterator<Entry<K, V>> iterator() {
return new EntryIterator();
}
} // ----------- end of nested EntryIterable class -----------
/**
* 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() {
return new EntryIterable();
}
public String toString()
{
return table.toString();
}
}