HashMap example in Java



Source : 

import java.util.*;
class HashMapDemo {
public static void main(String args[]) {
// Create a hash map
HashMap hm = new HashMap();
// Put elements to the map
hm.put("Neeth Damera", new Double(3434.34));
hm.put("Ranjith Nethi", new Double(123.22));
hm.put("Vemmula Veda Prakash", new Double(1378.00));
hm.put("Katti Venkataiah", new Double(99.22));
hm.put("Purumala Anil Kumar Reddy", new Double(-19.08));
// Get a set of the entries
Set set = hm.entrySet();
// Get an iterator
Iterator i = set.iterator();
// Display elements
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 Neeth Damera's account
double balance = ((Double)hm.get("Neeth Damera")).doubleValue();
hm.put("Neeth Damera", new Double(balance + 1000));
System.out.println("Neeth Damera's new balance: " +
hm.get("Neeth Damera"));
}
}


Execution : 


E:\>javac "hashmap testing.java"
Note: hashmap testing.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

E:\>java HashMapDemo
Vemmula Veda Prakash: 1378.0
Neeth Damera: 3434.34
Katti Venkataiah: 99.22
Ranjith Nethi: 123.22
Purumala Anil Kumar Reddy: -19.08

Neeth Damera's new balance: 4434.34






Comments