- HashMap contains values based on the key. It implements the Map interface and extends AbstractMap class.
- HashMap is used for storing Key & value pairs.
- HashMap contains only unique elements.
- HashMap may have one null key and multiple null values.
- HashMap doesn’t maintains insertion order.
- HashMap is similar to the Hashtable class except that it is unsynchronized and permits null values and null key.
- HashMap maintains key and value pairs and often denoted as HashMap<Key, Value> .
Constructors of HashMap class as shown below :
- HashMap (): It is a default constructors hashmap.
- HashMap (Map m): It initializes the hash map by using the elements of m.
- HashMap (int capacity): It initializes the capacity of the hash map to capacity. The default initial capacity of HashMap will be 16 and the load factor will be 0.75.Load factor represents at what level HashMap should be increase your capacity .
- HashMap (int capacity, float fillRatio): it initializes capacity and fillRatio to grow the capacity after certain elements inserted.
HashMap Example in Java:
- import java.util.*;
- class HapMapDemo{
- public static void main(String args[]){
- HashMap<Integer,String> hm=new HashMap<Integer,String>();
- hm.put(100,“A”);
- hm.put(101,“B”);
- hm.put(102,“C”);
- for(Map.Entry mentry : hm.entrySet()){
- System.out.println(mentry .getKey()+” “+mentry .getValue());
- }
- }
- }
Output:
102 C
100 A
101 B
HashMap Class Methods
- void clear(): It removes all the key and value pairs from the specified Map.
- Object clone(): It returns a copy of all the mappings of a map and used for cloning them into another map.
- boolean containsKey(Object key): It is a boolean function which returns true or false based on whether the specified key is found in the map.
- boolean containsValue(Object Value): Similar to containsKey() method, however it looks for the specified value instead of key.
- Value get(Object key): It returns the value for the specified key.
- boolean isEmpty(): It checks whether the map is empty. If there are no key-value mapping present in the map then this function returns true else false.
- Set keySet(): It returns the Set of the keys fetched from the map.
- value put(Key k, Value v): Inserts key value mapping into the map. Used in the above example.
- int size(): Returns the size of the map – Number of key-value mappings.
- Collection values(): It returns a collection of values of map.
- Value remove(Object key): It removes the key-value pair for the specified key. Used in the above example.
- void putAll(Map m): Copies all the elements of a map to the another specified map.
Another HashMap Example in Java:
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;
public class HashMapDemo1 {
public static void main(String args[]) {
// Declare HashMap
HashMap<Integer, String> hmap = new HashMap<Integer, String>();
// Adding elements to HashMap
hmap.put(11, "A");
hmap.put(20, "B");
hmap.put(17, "C");
hmap.put(10, "D");
hmap.put(31, "E");
// Display element using Iterator
Set set = hmap.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry mhm = (Map.Entry)iterator.next();
System.out.print("key = "+ mhm.getKey() + " & Value = "+mhm.getValue());
}
// Get values based on key
String var= hmap.get(20);
System.out.println("Value at index 2 is: "+var);
// Remove values based on key
hmap.remove(31);
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 = "+mentry2.getKey() + " & Value = "+mentry2.getValue());
}
}
}
Output:
key = 10 & Value = D
key = 20 & Value = B
key = 31 & Value = E
key = 17 & Value = C
key = 11 & Value = A
Value at index 2 is: B
Map key and values after removal:
Key = 10 & Value = D
Key = 20 & Value = B
Key = 17 & Value = C
Key = 11 & Value = A
[…] HashMap is non synchronized. which means multiple threads can work on HashMap at the same […]
LikeLike
[…] HashMap is an implementation of Map interface. […]
LikeLike