forked from Nnamdikeshi/Java2545examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMapSnow.java
More file actions
40 lines (26 loc) · 1.11 KB
/
Copy pathHashMapSnow.java
File metadata and controls
40 lines (26 loc) · 1.11 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
package week4_data_structures;
import java.util.HashMap;
/**
* Hello HashMap. Create and add key-value paits
*/
public class HashMapSnow {
public static void main(String[] args) {
HashMap snowfall = new HashMap();
snowfall.put("January", 3);
snowfall.put("February", 10);
Object janSnowfall = snowfall.get("January");
System.out.println("In January, " + janSnowfall + " inches of snow fell");
// If we need the January snowfall as an int or Integer, must cast
// Or (often better) use generic types when creating the HashMap
int intJanSnowfall = (int) snowfall.get("January");
int intFebSnowfall = (int) snowfall.get("February");
// Need data as int to do math
int total = intJanSnowfall + intFebSnowfall;
System.out.println("Total snow = " + total);
// Print all of the data
for (Object ob : snowfall.keySet()) {
System.out.println("Month: " + ob); // ob is a key
System.out.println("Snowfall inches: " + snowfall.get(ob)); // get(ob) is the value for that key
}
}
}