Java Examples
Java Examples
memu home questions

Basic HashMap Example

HashMap is an object that stores a key and value as a pairs.

Source: (

import java.util.*;
public class MyHashMap {
   public static void main(String args[]){
      HashMap<String, String> map = new HashMap<String, String>();
      map.put("Tom1", "Tom Jones");
      map.put("Tom2", "Tom Sawyer");
      map.put("Tom3", "Tom Hanks");
      System.out.println("Size of Map "+ map.size());
      System.out.println("Remove Tom Hanks");
      System.out.println("Size of Map "+ map.size());
      Iterator<String> it = map.keySet().iterator();
         String key =;
         System.out.println(key + " : " + map.get(key));


$ java MyHashMap 
Size of Map 3
Remove Tom Hanks
Size of Map 2
Tom2 : Tom Sawyer
Tom1 : Tom Jones