package collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class HashMapExample { public static void main(String[] args) { /* This is how to declare HashMap */ HashMap hmap = new HashMap(); /* Adding elements to HashMap */ hmap.put(12, "Core Java"); hmap.put(2, "Adv Java"); hmap.put(7, "Spring"); hmap.put(49, "Hibernate"); hmap.put(3, "Maven"); /* Get values based on key */ String var = hmap.get(2); // Key:2 System.out.println("Value for key:2 is: " + var + "\n"); /* Remove values based on Key:3 */ hmap.remove(3); /* Display content using Iterator */ Set> set = hmap.entrySet(); Iterator> iterator = set.iterator(); while (iterator.hasNext()) { Entry me = iterator.next(); System.out.print("Key is: " + me.getKey() + " & Value is: " + me.getValue() + "\n"); } } }