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.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…