hashmap example in java
HashMap is a Map-based collection class that is used for storing Key & value pairs, it is denoted as HashMap<Key, Value> or HashMap<K, V>. This class makes no guarantees as to the order of the map. It is similar to the Hashtable class except that it is unsynchronized and permits nulls(null values and null key).
It is not an ordered collection which means it does not return the keys and values in the same order in which they have been inserted into the HashMap. It does not sort the stored keys and Values. You must need to import java.util.HashMap
or its superclass in order to use the HashMap class and methods.
import java.util.HashMap; import java.util.Map; import java.util.Iterator; import java.util.Set; public class Details { public static void main(String args[]) { /* This is how to declare HashMap */ HashMap<Integer, String> hmap = new HashMap<Integer, String>(); /*Adding elements to HashMap*/ hmap.put(12, "Chaitanya"); hmap.put(2, "Rahul"); hmap.put(7, "Singh"); hmap.put(49, "Ajeet"); hmap.put(3, "Anuj"); /* Display content using Iterator*/ Set set = hmap.entrySet(); Iterator iterator = set.iterator(); while(iterator.hasNext()) { Map.Entry mentry = (Map.Entry)iterator.next(); System.out.print("key is: "+ mentry.getKey() + " & Value is: "); System.out.println(mentry.getValue()); } /* Get values based on key*/ String var= hmap.get(2); System.out.println("Value at index 2 is: "+var); /* Remove values based on key*/ hmap.remove(3); System.out.println("Map key and values after removal:"); Set set2 = hmap.entrySet(); Iterator iterator2 = set2.iterator(); while(iterator2.hasNext()) { Map.Entry mentry2 = (Map.Entry)iterator2.next(); System.out.print("Key is: "+mentry2.getKey() + " & Value is: "); System.out.println(mentry2.getValue()); } } }
Output:
key is: 49 & Value is: Ajeet key is: 2 & Value is: Rahul key is: 3 & Value is: Anuj key is: 7 & Value is: Singh key is: 12 & Value is: Chaitanya Value at index 2 is: Rahul Map key and values after removal: Key is: 49 & Value is: Ajeet Key is: 2 & Value is: Rahul Key is: 7 & Value is: Singh Key is: 12 & Value is: Chaitanya