-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMapDemo.java
More file actions
34 lines (32 loc) · 1.02 KB
/
HashMapDemo.java
File metadata and controls
34 lines (32 loc) · 1.02 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
import java.util.*;
class HashMapDemo {
public static void main(String args[]) {
// Create a hash map
HashMap<String, Double> hm = new HashMap<String, Double>();
// Put elements to the map
hm.put("John Doe", 3434.34);
hm.put("Tom Smith", 123.22);
hm.put("Jane Baker", 1378.00);
hm.put("Todd Hall", 99.22);
hm.put("Ralph Smith", -19.08);
// Get a set of the entries
System.out.println(hm);
System.out.println("Bollean value " + hm.containsKey("John Doe"));
Set set = hm.entrySet();
System.out.println(set.isEmpty());
// Get an iterator
Iterator i = set.iterator();
// Display elements
System.out.println(set);
while (i.hasNext()) {
Map.Entry me = (Map.Entry) i.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
// Deposit 1000 into John Doe's account
double balance = ((Double) hm.get("John Doe")).doubleValue();
hm.put("John Doe", new Double(balance + 1000));
System.out.println("John Doe's new balance: " + hm.get("John Doe"));
}
}