KaiquanMah commited on
Commit
9b9f849
·
verified ·
1 Parent(s): 3bf0ae7

HashMap<String, Integer>; put get containsKey keySet(= k-v pair)

Browse files
Week 3: Objects, files and exceptions/11a Hashmap ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ In addition to lists (and tables), a very useful data structure is a HASHMAP.
2
+ It closely resembles Python's scatter table (aka DICTIONARY),
3
+ the idea being that each element has a KEY and a VALUE - the value is determined by the key.
4
+
5
+ The elements are in NO particular ORDER - there is no question of first, second or last element.
6
+ Instead, we have a set of elements.
7
+ You can see this when you print the scatterplot - the elements can be PRINTED in ANY ORDER.
8
+
9
+
10
+
11
+ Let's first look at an example of a hashmap initialization and usage:
12
+
13
+ import java.util.HashMap;
14
+
15
+ public class Example {
16
+ public static void main(String[] args){
17
+ HashMap<String, Integer> heights = new HashMap<>();
18
+ heights.put("Jack", 172);
19
+ heights.put("Jane", 169);
20
+ heights.put("Kim", 181);
21
+ heights.put("Karl", 158);
22
+
23
+ System.out.println(heights);
24
+ }
25
+ }
26
+
27
+
28
+ Program outputs:
29
+ {Kim=181, Jack=172, Jane=169, Karl=158}
30
+
31
+
32
+
33
+
34
+ ==========================================================
35
+
36
+
37
+
38
+ In the example, you will notice that 2 generic type specifications are given when defining a hashmap: KEY TYPE and VALUE TYPE.
39
+
40
+ A new element can be inserted using the PUT method.
41
+ The method takes as parameters the key and the value.
42
+ If the key is NOT FOUND in the table, a new element is INSERTed.
43
+ If the key already EXISTS, the value of the element is REPLACED.
44
+
45
+
46
+ Similarly, the GET method can be used to return a value from a hashmap: the method takes the key as a parameter.
47
+
48
+
49
+ HashMap<String, Integer> heights = new HashMap<>();
50
+ heights.put("Jane", 169);
51
+ heights.put("Karl", 158);
52
+
53
+ System.out.println(heights.get("Karl"));
54
+ System.out.println(heights.get("Jane"));
55
+
56
+
57
+ // Karl grows
58
+ heights.put("Karl", 161); //UPDATE
59
+ System.out.println(heights.get("Karl"));
60
+
61
+
62
+
63
+ Program outputs:
64
+ 158
65
+ 169
66
+ 161
67
+
68
+
69
+
70
+
71
+ ==========================================================
72
+
73
+
74
+
75
+
76
+ If the get method does not find a value for the given key in the hashmap, it returns null, which is an empty value.
77
+
78
+
79
+
80
+
81
+ The containsKey method can be used to test whether the given key can be found in the hashmap:
82
+
83
+ HashMap<Integer, Double> squares = new HashMap<>();
84
+ squares.put(9, 3.0);
85
+ squares.put(4, 2.0);
86
+ squares.put(16, 4.0);
87
+
88
+
89
+
90
+ System.out.println(squares.get(9));
91
+ System.out.println(squares.get(4));
92
+ // this is not found
93
+ System.out.println(squares.get(10)); //null
94
+
95
+ System.out.println(squares.containsKey(16)); //true
96
+ System.out.println(squares.containsKey(15)); //false
97
+
98
+
99
+ Program outputs:
100
+ 3.0
101
+ 2.0
102
+ null
103
+ true
104
+ false
105
+
106
+
107
+
108
+
109
+
110
+ ==========================================================
111
+
112
+
113
+
114
+ Although a hashmap is primarily intended for situations where you know the key and can retrieve a value from it,
115
+ it is sometimes useful to iterate through all the elements of the table.
116
+
117
+ This is easily done with the keySet method:
118
+
119
+
120
+ HashMap<Integer, Double> squares = new HashMap<>();
121
+ squares.put(9, 3.0);
122
+ squares.put(4, 2.0);
123
+ squares.put(16, 4.0);
124
+ squares.put(25, 5.0);
125
+
126
+ for (int key : squares.keySet()) {
127
+ System.out.println(key + ": " + squares.get(key));
128
+ }
129
+
130
+ Program outputs:
131
+ 16: 4.0
132
+ 4: 2.0
133
+ 9: 3.0
134
+ 25: 5.0
135
+
136
+
137
+
138
+
139
+
140
+
141
+