-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMap151.java
executable file
·121 lines (107 loc) · 3 KB
/
Map151.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
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
/**
* Implements the interface. A concrete implementaion of a map with all of the
* ugly search
*
* @author gtowell Created: Sep 21, 2020 Modified: Jul 2021
* @author trang
* Modified: Oct 6, 2021 to add main method for storing unique words and
* their occurences of a file.
*/
public class Map151<K, V> implements Map151Interface<K, V> {
/** The underlying data structure to actually do the storage */
private ArrayList<Pair<K, V>> underlying = new ArrayList<>();
/**
* Holds key-value pairs together The only time it is OK (or even good) to have
* public instance vars is in private classes. (Since they are not really public
* anyway)
*/
private class Pair<L, W> {
// the key. Once set it canot be changed
public final L key;
// the value
public W value;
// Create a key value pair.
Pair(L ky, W val) {
key = ky;
value = val;
}
}
/**
* Set a key value pair. Will overwrite value if key already present.
*
* @param key the key
* @param val the new value
*/
public void put(K key, V val) {
Pair<K, V> pair = iContainsKey(key);
if (pair == null) {
Pair<K, V> np = new Pair<>(key, val);
underlying.add(np);
} else {
pair.value = val;
}
}
/**
* Get the value associated with a key
*
* @param key the key
* @return the associated value
*/
public V get(K key) {
Pair<K, V> pair = iContainsKey(key);
if (pair != null)
return pair.value;
return null;
}
/**
* does the Map contain the key?
*
* @param key the key in question
* @return true iff the key is in the map
*/
public boolean containsKey(K key) {
return null != iContainsKey(key);
}
private Pair<K, V> iContainsKey(K ky) {
for (Pair<K, V> pair : underlying) {
if (pair.key.equals(ky)) {
return pair;
}
}
return null;
}
/**
* The number of items in the map
*
* @return The number of items in the map
*/
public int size() {
return underlying.size();
}
/**
* All of the keys in the map This method allows users of the map to see all of
* the keys. That this returns a Set is to make this consistent with the Java
* Map interface.
*
* @return All of the keys in the map
*/
public Set<K> keySet() {
TreeSet<K> set = new TreeSet<>();
for (Pair<K, V> pair : underlying) {
set.add(pair.key);
}
return set;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
for (Pair<K, V> pair : underlying) {
sb.append("[" + pair.key + ":" + pair.value + "]\n");
}
return sb.toString();
}
}