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
public class AlphaMapLL implements Map<Integer,String> {
  private class Node {
    int key;
    String value;
    Node next = null;
 
    public Node(int key, String value) {
      this.key = key;
      this.value = value;
    }
  }
 
  private Node head = null;
 
  public AlphaMapLL() {
    // nothing to do here; head is default assigned to null.
  }
 
  @Override
  public String get(Integer key) {
    return getFrom(this.head, key);
  }
 
  private String getFrom(Node uu, int needle) {
    if (uu == null) return null;
    else if (needle == uu.key) return uu.value;
    else return getFrom(uu.next, needle);
  }
 
  @Override
  public void set(Integer key, String value) {
    /* Note: There is something subtle going on here.
     * If the key is already in the linked list, you might think
     * that we have to traverse the list, find the node with the key,
     * and change its value. But not so! Since the get method will
     * just return the first value it comes to, we can just store
     * the same key multiple times in the linked list; the most recent
     * storage is what get(key) will return.
     * (Unfortunately, you can't use this trick in a Binary Search Tree!)
     */
    Node newNode = new Node(key, value);
    newNode.next = this.head;
    this.head = newNode;
  }
 
  @Override
  public int size() {
    throw new UnsupportedOperationException("it's hard to avoid over-counting here!");
  }
}