HashMap Collection of Java

Source: Internet
Author: User

/**
* 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.