Hash Map
import
java.util.HashMap;
import
java.util.Iterator;
import
java.util.Map;
import
java.util.Set;
public class
HashMapTest {
public static void
main(String[] args) {
Map map =
new
HashMap();
map.put(
new
Integer(
1
),
"MCA"
);
map.put(
new
Integer(
2
),
"MBA"
);
map.put(
new
Integer(
3
),
"BSc"
);
map.put(
new
Integer(
4
),
"MSc"
);
map.put(
new
Integer(
5
),
"MCA"
);
System.out.println(
"Map Values Before: "
);
Set keys = map.keySet();
for
(Iterator i = keys.iterator(); i.hasNext();) {
Integer key = (Integer) i.next();
String value = (String) map.get(key);
System.out.println(key +
" = "
+ value);
}
System.out.println(
"\nRemove element with key 6"
);
map.remove(
new
Integer(
6
));
System.out.println(
"\nMap Values After: "
);
keys = map.keySet();
for
(Iterator i = keys.iterator(); i.hasNext();) {
Integer key = (Integer) i.next();
String value = (String) map.get(key);
System.out.println(key +
" = "
+ value);
}
}