File size: 3,070 Bytes
9b9f849
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b35b78
9b9f849
 
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
In addition to lists (and tables), a very useful data structure is a HASHMAP. 
It closely resembles Python's scatter table (aka DICTIONARY), 
the idea being that each element has a KEY and a VALUE - the value is determined by the key.

The elements are in NO particular ORDER - there is no question of first, second or last element.
Instead, we have a set of elements. 
You can see this when you print the scatterplot - the elements can be PRINTED in ANY ORDER.



Let's first look at an example of a hashmap initialization and usage:

import java.util.HashMap;

public class Example {
    public static void main(String[] args){
        HashMap<String, Integer> heights = new HashMap<>();
        heights.put("Jack", 172);
        heights.put("Jane", 169);
        heights.put("Kim", 181);
        heights.put("Karl", 158);

        System.out.println(heights);
    }
}


Program outputs:
{Kim=181, Jack=172, Jane=169, Karl=158}
 



==========================================================



In the example, you will notice that 2 generic type specifications are given when defining a hashmap: KEY TYPE and VALUE TYPE. 

A new element can be inserted using the PUT method. 
The method takes as parameters the key and the value. 
If the key is NOT FOUND in the table, a new element is INSERTed. 
If the key already EXISTS, the value of the element is REPLACED.


Similarly, the GET method can be used to return a value from a hashmap: the method takes the key as a parameter.


HashMap<String, Integer> heights = new HashMap<>();
heights.put("Jane", 169);
heights.put("Karl", 158);

System.out.println(heights.get("Karl"));
System.out.println(heights.get("Jane"));


// Karl grows
heights.put("Karl", 161); //UPDATE
System.out.println(heights.get("Karl"));



Program outputs:
158
169
161
 



==========================================================




If the get method does not find a value for the given key in the hashmap, it returns null, which is an empty value.




The containsKey method can be used to test whether the given key can be found in the hashmap:

HashMap<Integer, Double> squares = new HashMap<>();
squares.put(9, 3.0);
squares.put(4, 2.0);
squares.put(16, 4.0);



System.out.println(squares.get(9));
System.out.println(squares.get(4));
// this is not found
System.out.println(squares.get(10)); //null

System.out.println(squares.containsKey(16)); //true
System.out.println(squares.containsKey(15)); //false


Program outputs:
3.0
2.0
null
true
false





==========================================================



Although a hashmap is primarily intended for situations where you know the key and can retrieve a value from it, 
it is sometimes useful to iterate through all the elements of the table. 

This is easily done with the keySet method:


HashMap<Integer, Double> squares = new HashMap<>();
squares.put(9, 3.0);
squares.put(4, 2.0);
squares.put(16, 4.0);
squares.put(25, 5.0);

for (int key : squares.keySet()) {
    System.out.println(key + ": " + squares.get(key));
}

Program outputs:
16: 4.0
4: 2.0
9: 3.0
25: 5.0