-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.java
More file actions
56 lines (50 loc) · 1.24 KB
/
Map.java
File metadata and controls
56 lines (50 loc) · 1.24 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
import java.util.ArrayList;
/**
* A simple mapping of keys to values that preserves the order in which the
* key value pairs are added
*
* @author Chris Blades
* @version 16/3/2010
*/
public class Map<K, V> {
/** List of keys */
private ArrayList<K> keys;
/** List of values */
private ArrayList<V> values;
/**
* Creates a new Map with no key-value pairs
*/
public Map() {
this.keys = new ArrayList<K>();
this.values = new ArrayList<V>();
}
/**
* Inserts a new key-value pair.
*
* @param key the new key to add
* @param value the new value to add
*/
public void put(K key, V value) {
keys.add(key);
values.add(value);
}
/**
* Removes all key-value pairs from the Map
*/
public void clear() {
keys.clear();
values.clear();
}
/**
* Returns a stirng containing all key-value pairs in this Map
*
* @string description of all key-value pairs in this Map
*/
public String toString() {
String description = "";
for (int i = 0; i < keys.size(); i++) {
description += " {" + keys.get(i) + " ==> " + values.get(i) +"} ";
}
return description;
}
}