MCPcopy Create free account
hub / github.com/Berkeley-CS61B/skeleton-sp23 / ULLMap

Class ULLMap

lab07/src/ULLMap.java:8–139  ·  view source on GitHub ↗

A data structure that uses a linked list to store pairs of keys and values. Any key must appear at most once in the dictionary, but values may appear multiple times. Key operations are get(key), put(key, value), and contains(key) methods. The value associated to a key is the value in the last cal

Source from the content-addressed store, hash-verified

6 * times. Key operations are get(key), put(key, value), and contains(key) methods. The value
7 * associated to a key is the value in the last call to put with that key. */
8public class ULLMap<K, V> implements Map61B<K, V> {
9
10 int size = 0;
11
12 /** Returns the value corresponding to KEY or null if no such value exists. */
13 public V get(K key) {
14 if (list == null) {
15 return null;
16 }
17 Entry lookup = list.get(key);
18 if (lookup == null) {
19 return null;
20 }
21 return lookup.val;
22 }
23
24 @Override
25 public int size() {
26 return size;
27 }
28
29 /** Removes all of the mappings from this map. */
30 @Override
31 public void clear() {
32 size = 0;
33 list = null;
34 }
35
36 /** Inserts the key-value pair of KEY and VALUE into this dictionary,
37 * replacing the previous value associated to KEY, if any. */
38 public void put(K key, V val) {
39 if (list != null) {
40 Entry lookup = list.get(key);
41 if (lookup == null) {
42 list = new Entry(key, val, list);
43 } else {
44 lookup.val = val;
45 }
46 } else {
47 list = new Entry(key, val, list);
48 size = size + 1;
49 }
50 }
51
52 /** Returns true if and only if this dictionary contains KEY as the
53 * key of some key-value pair. */
54 public boolean containsKey(K key) {
55 if (list == null) {
56 return false;
57 }
58 return list.get(key) != null;
59 }
60
61 @Override
62 public Iterator<K> iterator() {
63 return new ULLMapIter();
64 }
65

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected