-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyMap.java
More file actions
115 lines (113 loc) · 3.01 KB
/
KeyMap.java
File metadata and controls
115 lines (113 loc) · 3.01 KB
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
import java.util.Iterator;
import java.util.NoSuchElementException;
/*
public class KeyMap constructs a linked key/value pair set, with no
duplicates and a restricted list of methods
created by Liam Murphy
*/
public class KeyMap<Integer> implements KMap<Integer>{
/* private inner class Node for each element of the linked chain */
private class Node{
/* Node instance variables */
private Integer key;
private Integer value;
private Node next;
/* Node constructor */
public Node(Integer key, Integer value){
this.key = key;
this.value = value;
this.next = null;
}
/* Node toString method */
public String toString(){
String s = key + "=" + value;
return s;
}
}
/* KeyMap instance variables */
private Node head;
private int size;
/* KeyMap constructor */
public KeyMap(){
this.head = null;
this.size = 0;
}
/* add takes in a key and a value and adds them if they do not already exist,
returning true upon a successful add and false otherwise */
public boolean add(Integer key, Integer value){
Node current = head;
while(current != null){
if(key.equals(current.key)){
return false;
}
current = current.next;
}
Node newNode = new Node(key, value);
newNode.next = head;
head = newNode;
size++;
return true;
}
/* get takes in a key and searches the KeyMap for this key, returning the
corresponding value if found, and null otherwise */
public Integer get(Integer key){
Node current = head;
while(current != null){
if(key.equals(current.key)){
return current.value;
}
current = current.next;
}
return null;
}
/* size takes in unit and returns the size */
public int size(){
return size;
}
/* KeyMap toString method */
public String toString(){
Node current = head;
String s = "";
while (current != null){
s = s + (current.toString()) + "\n";
current = current.next;
}
return s;
}
/* iterator returns a new KeyIterator */
public Iterator<Integer> iterator(){
return new KeyIterator();
}
/* private inner class Iterator */
private class KeyIterator implements Iterator<Integer>{
/* Iterator instance variables */
private Node on;
private int count;
/* Iterator constructor */
public KeyIterator(){
this.on = head;
this.count = 0;
}
/* hasNext takes in unit and returns true if there is another element */
public boolean hasNext(){
boolean b = (on != null);
return b;
}
/* next takes in unit and returns the element at the on position */
public Integer next(){
if (hasNext()){
Integer key = on.key;
on = on.next;
count++;
return key;
}
else{
throw new NoSuchElementException("How did you get here ??");
}
}
/* Unsupported method: remove() */
public void remove(){
throw new UnsupportedOperationException("How did you try to use this ??");
}
}
}