HashMap Example Java

Three programs are given on HashMap with different functionalities used very often in coding.

  1. HashMap General: In this program, API and the general operations like adding, retrieving and deleting elements, size and checking the existence of elements are done.
  2. HashMap Iteration: The iteration styles of elements, adding elements of one HashMap to another and cloning are shown.
  3. HashMap Sort: Sorting operations on keys and values are done. Special attention is paid to retrieve the key when a value is given.

Note: It is advised to read the tutorial on HashMap before attempting the above programs as it gives the knowledge on features, methods and super classes of HashMap etc.

It is the first program of the above three.

Example on HashMap Example Java
import java.util.*;
public class HashMapGeneral
{
  public static void main(String args[])
  {
    HashMap hm1 = new HashMap();

    System.out.println("Elements does not exist: " + hm1.isEmpty());

    hm1.put("apple", "red");
    hm1.put(10, 20);
    hm1.put(null, null);
    hm1.put("birthDate", new Date().getDate());
    hm1.put("apple", "green");
    System.out.println("Elements does not exist: " + hm1.isEmpty());

    System.out.println("\napple key exists: " + hm1.containsKey("apple"));
    System.out.println("red value exists: " + hm1.containsValue("red"));


    System.out.println("Value of birthDate key: " + hm1.get("birthDate"));
    System.out.println("Value of apple key: " + hm1.get("apple"));

    System.out.println("\nNo. of elements before 10 key removal: " + hm1.size());
    System.out.println("Elements of hm1 before removal: " + hm1);
    hm1.remove(10);
    System.out.println("No. of elements after 10 key removal: " + hm1.size());
    System.out.println("Elements of hm1 after 10 key removal: " + hm1);

    hm1.clear();
    System.out.println("\nNo. of elements after clear(): " + hm1.size());
    System.out.println("Elements of hm1 after clear(): " + hm1);
  }
}


HashMap Example Java
Output screenshot on HashMap Example Java

HashMap hm1 = new HashMap();
System.out.println(“Elements does not exist: ” + hm1.isEmpty());

A non-generic HashMap object hm1 is created and is checked its emptiness. In the above statement, isEmpty() returns true as no elements exist still with hm1. The same method if called after elements are added returns false (see next statements).

hm1.put(“apple”, “red”);
hm1.put(null, null);
hm1.put(“apple”, “green”);

With put() method, a number of key/value pairs (elements) are added. We know in HashMap Tutorial, HashMap permits null keys and null values. HashMap does not accept duplicate keys. If added, the earlier value is overridden. Observe, the "apple" key is added twice with values "red" and later "green". Red is overridden with green. Now "green" remains with the key apple.

System.out.println(“\napple key exists: ” + hm1.containsKey(“apple”));
System.out.println(“red value exists: ” + hm1.containsValue(“red”));

Using containsKey() and containsValue(), a programmer can check the existence of a key or value with the HashMap. "apple" returns true and "red" returns false (notice, "red" is replaced by "green").

System.out.println(“Value of birthDate key: ” + hm1.get(“birthDate”));
System.out.println(“Value of apple key: ” + hm1.get(“apple”));

Using get() method (very frequently used method), the value associated with a key can be retrieved. Observe, "apple" prints green and not red (as red is overridden by green).

System.out.println(“\nNo. of elements before 10 key removal: ” + hm1.size());
hm1.remove(10);
hm1.clear();

The size() method returns the number of elements (key/value pairs) present in the HashMap. remove() and clear() are used to remove the elements, but with a small difference. The method remove() deletes only one element and clear() deletes all at a time.

Pass your comments and suggestions on this tutorial "HashMap Example Java".

Leave a Comment

Your email address will not be published.