Since Java 1.2, HashMap<K, V> class has been part of the Java collection. This class can be found in java.util package. It implements the Java Map interface in its most basic form. If you try to insert the duplicate key, it will replace the associated key’s element. The key index makes it simple to conduct actions such as updating, deleting, etc.
HashMap is similar to HashTable except that it is not synchronized. It also allows for the storage of null keys, however, there should only be one null key object and an unlimited number of null values.
public class HashMap<K,V>extends AbstractMap<K,V>implements Map<K,V>,Cloneable, Serializable
HashMap has four constructors, each with a public access modifier, which are as follows:
1)HashMap() :
HashMap<K, V> hm = new HashMap<K, V>();
2)HashMap (int initialCapacity):
HashMap<K, V> hm = new HashMap<K, V>(int initialCapacity);
3)HashMap (int initialCapacity, float loadFactor):
HashMap<K, V> hm = new HashMap<K, V>(int initialCapacity, int loadFactor);
4)HashMap (Map map):
HashMap<K, V> hm = new HashMap<K, V>(Map map);
import java.io.*;
import java.util.*;
class Coderz {
public static void main(String args[])
{
Map<Integer, String> hmobj = new HashMap<>();
// Add Elements using put method
hmobj.put(1, "A");
hmobj.put(2, "B");
hmobj.put(3, "C");
// Initialization of a HashMap
// using Generics
HashMap<Integer, String> hmobj2
= new HashMap<Integer, String>(hmobj);
System.out.println("Mappings of HashMap hmobj are : "
+ hmobj);
System.out.println("Mapping of HashMap hmobj2 are : "
+ hmobj);
}
}
Mappings of HashMap hmobj are : {1=A, 2=B, 3=C}
Mapping of HashMap hmobj2 are : {1=A, 2=B, 3=C}
import java.io.*;
import java.util.*;
class Coderz {
public static void main(String args[])
{
// Initialization of a HashMap
HashMap<Integer, String> hobj
= new HashMap<Integer, String>();
// Change Value using put method
hobj.put(1, "A");
hobj.put(2, "B");
hobj.put(3, "C");
System.out.println("Initial Map " + hobj);
hobj.put(2, "E");
System.out.println("Updated Map " + hobj);
}
}
Initial Map {1=A, 2=B, 3=C}
Updated Map {1=A, 2=E, 3=C}
import java.io.*;
import java.util.*;
class Coderz {
public static void main(String args[])
{
// Initialization of a HashMap
HashMap<Integer, String> hobj
= new HashMap<Integer, String>();
// Change Value using put method
hobj.put(1, "A");
hobj.put(2, "B");
hobj.put(3, "C");
System.out.println("Initial Map " + hobj);
hobj.remove(2);
System.out.println("Updated Map " + hobj);
}
}
Initial Map {1=A, 2=B, 3=C}
Updated Map {1=A, 3=C}
import java.io.*;
import java.util.*;
class Coderz {
public static void main(String args[])
{
// Initialization of a HashMap
HashMap<Integer, String> hobj
= new HashMap<Integer, String>();
// Change Value using put method
hobj.put(1, "A");
hobj.put(2, "B");
hobj.put(3, "C");
for (Map.Entry<Integer, String> e : hobj.entrySet())
System.out.println("Key: " + e.getKey()
+ " Value: " + e.getValue());
}
}
Key: 1 Value: A
Key: 2 Value: B
Key: 3 Value: C
Note: also read about the LinkedHashSet class in Java Collection Framework
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…