HashMap Example in Java Collection Framework


HashMap Example in Java Collection Framework. This Java HashMap example describes the basic operations performed on the HashMap in Collection Framework.

Program:

import java.util.*;
class HashMapDemo{
public static void main(String[] args) {
HashMap hm = new HashMap();
hm.put(“Emp Name”, “EshuSoft”);
hm.put(“Designation”, “Technical Manager”);
hm.put(“Company”, “EshuSoft Technologies”);
hm.put(“Location”, “Bangalore”);
System.out.println(“Company:” + hm.get(“Company”));
System.out.println(“HashMap Values:”);
System.out.println(hm);
Set s = hm.entrySet();
Iterator it = s.iterator();
while (it.hasNext()) {
Map.Entry me = (Map.Entry) it.next();
System.out.println(“key: ” + me.getKey() + ”  value:” + me.getValue());
}
}
}

Result:

Company:EshuSoft Technologies
HashMap Values:
{Emp Name=EshuSoft, Designation=Technical Manager, Company=EshuSoft Technologies, Location=Bangalore}
key: Emp Name  value:EshuSoft
key: Designation  value:Technical Manager
key: Company  value:EshuSoft Technologies
key: Location  value:Bangalore

2 Comments

  • 1
    Hasin
    July 31, 2012 - 5:49 pm | Permalink

    This HashMap example looks very simple and easy to understand. thanks for your example.

  • 2
    J Jagadeesh Kumar
    December 10, 2012 - 5:22 pm | Permalink

    I Have to find this type output and what is the source code for this output
    OUTPUT:
    Enter the number of Countries to Enter:3
    Enter Country Id:1
    Enter Country Name:India
    ENTER ANOTHER COUNTRY
    Enter Country Id:2
    Enter Country Name:AUS
    ENTER ANOTHER COUNTRY
    Enter Country Id:3
    Enter Country Name:USA
    Enter NO.OF STATES TO THE CORRESPONDING COUNTRIES:6
    Enter Country Id:1
    Enter State:AP,UP,TN
    Enter Country Id:2
    Enter State:syd,mus,ghr
    Enter Country Id:3
    Enter State: meb,ske,sdi
    U R Entered The Countries with Corresponding States as given bellow and the countries are in Sorting order
    AUS:syd,mus,ghr
    IND:AP,UP,TN
    USA:meb,ske,sdi

    Please send the above output source code to my email-id