-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMap1D.java
More file actions
108 lines (91 loc) · 2.65 KB
/
HashMap1D.java
File metadata and controls
108 lines (91 loc) · 2.65 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
/**
* Author: Andrew Laing
* Email: parisianconnections@gmail.com
* Date: 26/12/2016.
*/
import java.util.HashMap;
import java.util.List;
import java.io.Serializable;
// TODO: 22/12/2016 Add a copy constructor
public class HashMap1D extends HashMap implements Serializable
{
private HashMap<String, Integer> map;
/**
* Constructor
*/
public HashMap1D()
{
map = new HashMap<String, Integer>();
}
/**
* The method addElement adds an element to the HashMap
* @param key1 The key of the element
* @param value The value attached to the key
*/
public void addElement(String key1, Integer value)
{
// If the key does not exist create it
if (!map.containsKey(key1)) {
map.put(key1, value);
}
}
/**
* The method getElement returns the value attached to the key
* passed to the method. If the key does not exist it adds a new
* element for the key with a value of 0 to the HashMap.
* @param key1 The key to get the value for.
* @return The value attached to the key.
*/
public Integer getElement(String key1)
{
if (!map.containsKey(key1)) {
map.put(key1, 0);
}
return map.get(key1);
}
/**
* The method augment increases the value attached to the key by 1.
* @param key1 The key to augment the value for.
*/
public void augment(String key1)
{
map.put(key1, (getElement(key1) + 1));
}
/**
* The method getKeys adds all of the keys within the HashMap
* to the List passed to it.
* @param keys A List to add all of the keys in the HashMap to.
*/
public void getKeys(List<String> keys)
{
for (String key : map.keySet()) {
keys.add(key);
}
}
/**
* The method getValue returns the value attached to the key
* passed to the method. If the key does not exist it returns 0.
* @param key1 The key to get the value for.
* @return The value attached to the key.
*/
public Integer getValue(String key1)
{
if (!map.containsKey(key1)) {
return 0;
}
return map.get(key1);
}
/**
* The method getSumOfValues gets the sum of all of the values
* stored within the HashMap.
* @return The sum of all of the values stored within the HashMap.
*/
public int getSumOfValues()
{
int sum = 0;
for (String key : map.keySet()) {
sum += map.get(key);
}
return sum;
}
}