HashMap is a part of java.util package.HashMap extends an abstract class AbstractMap which also provides an incomplete implementation of Map interface. It stores the data in (Key, Value) pairs.
We can initialize HashMap using the constructor in four different ways :
1.HashMap()
It is the default constructor with initial capacity 16 and load factor 0.75.
HashMap hs=new HashMap();
// Java program to demonstrate simple initialization // of HashMap import java.util.*; public class GFG { public static void main(String args[]) { HashMap<Integer, String> hm = new HashMap<Integer, String>(); hm.put(1, "Ram"); hm.put(2, "Shyam"); hm.put(3, "Sita"); System.out.println("Values " + hm); } } |
Values {1=Ram, 2=Shyam, 3=Sita}
2.HashMap(int initialCapacity)
It is used to create an empty HashMap object with specified initial capacity under default load factor 0.75.
HashMap hs=new HashMap(10);
// Java program to demonstrate initialization // of HashMap with given capacity. import java.util.*; public class GFG { public static void main(String[] args) { HashMap<Integer, String> hm = new HashMap<Integer, String>(3); hm.put(1, "C"); hm.put(2, "C++"); hm.put(3, "Java"); System.out.println("Values of hm" + hm); } } |
Values of hm{1=C, 2=C++, 3=Java}
3.HashMap(int initial capacity, float loadFactor)
It is used to create an empty HashMap object with specified initial capacity as well as load factor.
HashMap hs=new HashMap(10, 0.5);
// Java program to demonstrate initialization // of HashMap with given capacity and load factor. import java.util.*; public class GFG { public static void main(String[] args) { HashMap<Integer, String> hm = new HashMap<Integer, String>(3, 0.5f); hm.put(1, "C"); hm.put(2, "C++"); hm.put(3, "Java"); System.out.println("Values of hm" + hm); } } |
Values of hm{1=C, 2=C++, 3=Java}
4.HashMap(Map map)
It is used to initializes the hash map by using the elements of the given Map object map.
HashMap hs=new HashMap(map);
// Java program to demonstrate initialization // of HashMap from another HashMap. import java.util.*; public class GFG { public static void main(String[] args) { HashMap<Integer, String> hm = new HashMap<Integer, String>(); hm.put(1, "C"); hm.put(2, "C++"); hm.put(3, "Java"); System.out.println("Values of hm" + hm); // Creating a new map from above map HashMap<Integer, String> language = new HashMap<Integer, String>(hm); System.out.println("Values of language " + language); } } |
Values of hm{1=C, 2=C++, 3=Java}
Values of language {1=C, 2=C++, 3=Java}
Initializing Immutable HashMap
Please see Immutable Map in Java
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to Initialize and Compare Strings in Java?
- Initialize an ArrayList in Java
- KeyPairGenerator initialize() method in Java with Examples
- Initialize a static map in Java with Examples
- Initialize a static Map using Stream in Java
- Initialize a static Map using Java 9 Map.of()
- Initialize a static Map in Java using Double Brace Initialization
- Initialize a list in a single line with a specified value using Java Stream
- Different ways to Initialize all members of an array to the same value in C
- Best way to initialize empty array in PHP
- Python - Initialize empty array of given length
- Initialize a list in a single line with a specified value
- HashMap and TreeMap in Java
- HashMap Class Methods in Java with Examples | Set 1 (put(), get(), isEmpty() and size())
- Hashmap methods in Java with Examples | Set 2 (keySet(), values(), containsKey()..)
- Hashmap vs WeakHashMap in Java
- Internal Working of HashMap in Java
- HashMap in Java with Examples
- Differences between HashMap and HashTable in Java
- Differences between TreeMap, HashMap and LinkedHashMap in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

