Java Tutorial-Java HashMap

Java HashMap


A HashMap store items in “key/value” pairs and access them by an index of another type (example: a String). One object is used as a key (index) to another object (value). HashMap can store different types: String keys and Integer values, String keys and String values, Integer keys and Integer values, etc.


Example


Create a HashMap object called capitalCities which stores String keys and String values:

import java.util.HashMap; //import the HashMap class
 
public class HashMapExample {
       public static void main (String args[]){
             
              //Create a HashMap object called capitalCities
              HashMap<String, String> capitalCities = new HashMap<String, String>();
             
              capitalCities.put("San Francisco", "California");
              capitalCities.put("Chicago", "Illinois");
              capitalCities.put("Florida", "Tallahassee");
             
              System.out.println(capitalCities);
       }
}

Output

{San Francisco=California, Chicago=Illinois, Florida=Tallahassee}

Access an Item

To access a value in HashMap, we use get() method and refer to its key.


Example

capitalCities.get("Chicago");

Remove an Item

To remove an item from the HashMap, we use remove() method and refer to the key.


Example

capitalCities.remove("Florida");

To remove all the elements in HashMap, we use clear() method.


Example

capitalCities.clear();

HashMap Size

To find out how many elements in HashMap, we use size() method.


Example

capitalCities.size();

Loop through a HashMap

We can loop through a HashMap with a for-each loop.


Note: We use the keyset() method if we need to print only the keys and we use the values() method if we need to print only the values.


Example

import java.util.HashMap; //import the HashMap class
 
public class HashMapExample {
       public static void main (String args[]){
             
              //Create a HashMap object called capitalCities
              HashMap<String, String> capitalCities = new HashMap<String, String>();
             
              capitalCities.put("San Francisco", "California");
              capitalCities.put("Chicago", "Illinois");
              capitalCities.put("Florida", "Tallahassee");
             
              System.out.println("State ===>");
              for(String i : capitalCities.keySet()){
                     System.out.println(i); //print only keys
              }
             
              System.out.println("Capital cities ===>");
              for(String i : capitalCities.values()){
                     System.out.println(i);  //print only values
              }
       }
}

Output

State ===>
San Francisco
Chicago
Florida
Capital cities ===>
California
Illinois
Tallahassee

We can also print both keys and values in HashMap using for each loop.


Example

//Print both keys and values
for(String i : capitalCities.keySet()){
       System.out.println("key : "+ i + ", value : "+capitalCities.get(i));
}

Output

key : San Francisco, value : California
key : Chicago, value : Illinois
key : Florida, value : Tallahassee

Other Types

In HashMap, both keys and values are actually objects. In the above example, we used “String” objects. In Java, we have String is an object (not a primitive type). To use other types, such as int, we need to specify an equivalent wrapper class Integer. For other primitives, we use Boolean for boolean type, Character for char, Double for double, etc


Example

import java.util.HashMap;
 
public class MyHashMapExample {
       public static void main (String args[]){
              //Create a HashMap object called employee
              HashMap<Integer, String> employee = new HashMap<Integer, String>();
             
              //Add keys and values for employee
              employee.put(123, "John");
              employee.put(124, "Michael");
              employee.put(125, "Joe");
              System.out.println(employee);
       }
}

Output

{123=John, 124=Michael, 125=Joe}