/**
* HashMap Collection Explanation
* HashMap collection does not allow key duplication of collection elements
*/
Package com.test;
Import java.util.*;
public class Test8 {
/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
HashMap HM = new HashMap ();
Employer EMP1 = new employer ();
Emp1.setname ("xx");
Emp1.setage (22);
Emp1.setsalary (2000.00F);
Employer EMP2 = new employer ();
Emp1.setname ("zz");
Emp1.setage (22);
Emp1.setsalary (4000.00F);
Adding objects to the HashMap collection
Hm.put ("001", EMP1);
Hm.put ("002", EMP2);
if (Hm.containskey ("001"))
{
Employer TEMP = (employer) hm.get ("001");
SYSTEM.OUT.PRINTLN ("Personal Information:" + "Name:" +temp.getname () + "Age:" +temp.getage () + "Salary:" +temp.getsalary ());
}
Else
{
System.out.println ("No such employee");
}
SYSTEM.OUT.PRINTLN ("--------------use iterators to traverse the HashMap collection-----------------");
Using iterators to traverse the HashMap collection
Iterator it = Hm.keyset (). Iterator ();
while (It.hasnext ())//probing for the existence of the next element, returns a bool value
{
Remove key
String key = It.next (). toString ();
Remove value from key
Employer temp_x = (employer) hm.get (key);
SYSTEM.OUT.PRINTLN ("Personal Information:" + "Name:" +temp_x.getname () + "Age:" +temp_x.getage () + "Salary:" +temp_x.getsalary ());
}
}
}
Class Employer
{
private String name;
private int age;
private float salary;
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
public float getsalary () {
return salary;
}
public void Setsalary (float salary) {
This.salary = salary;
}
}
Summary:
HashMap Collection of Java