Parameters for Hashmap Class
HashMap<K,V>
It takes two parameters, K and V, while construction, namely as follows:
K: The type of keys maintained by this map
V: The type of mapped values
Load Factor
The Load Factor is a factor used internally by HashMap to decide whether the size of the Bucket array has to be raised. It is 0.75 by default. When the number of nodes in the HashMap exceeds 75% of its total capacity, HashMap expands the size of its bucket array. When HashMap's bucket array size has to be extended, its capacity is always doubled.
Initial Capacity
The Initial Capacity is effectively the number of buckets in the HashMap, set to 24 = 16 by default.
Also see, Swap Function in Java
Initialization and Constructors for Hashmap
HashMap class provides four types of constructors as follow:
- HashMap()
- HashMap(int initialCapacity)
- HashMap(int initialCapacity, float load factor)
- HashMap(Map map)
Let us study each of them in-depth.
Hashmap()
It is the default constructor which creates a Hashmap with default properties.
Hashmap has a load factor of 0.75 and a capacity of 16.
Syntax
HashMap<K, V> hash = new HashMap<K, V>();
You can also try this code with Online Java Compiler
Run Code
Example
import java.io.*;
import java.util.*;
class Main {
public static void main(String args[])
{
HashMap<Integer, String> map1 = new HashMap<Integer, String>();
map1.put(1, "coding");
map1.put(2, "ninjas");
System.out.println("Mappings of HashMap map1 are : "+ map1);
}
}
You can also try this code with Online Java Compiler
Run Code
Output
Mappings of HashMap map1 are : {1=coding, 2=ninjas}
You can also try this code with Online Java Compiler
Run Code
You can also read about the Java Destructor.
HashMap(int initialCapacity)
It creates an instance of HashMap with the specified initial capacity. The load factor is 0.75 by default.
Syntax
HashMap<K, V> hash = new HashMap<K, V>(int initialCapacity);
You can also try this code with Online Java Compiler
Run Code
Example
import java.io.*;
import java.util.*;
class Main {
public static void main(String args[])
{
HashMap<Integer, String> map1 = new HashMap<Integer, String>(2);
map1.put(1, "coding");
map1.put(2, "ninjas");
System.out.println("Mappings of HashMap map1 are : "+ map1);
}
}
You can also try this code with Online Java Compiler
Run Code
Output
Mappings of HashMap map1 are : {1=coding, 2=ninjas}
You can also try this code with Online Java Compiler
Run Code
HashMap(int initialCapacity, float loadFactor)
It creates a HashMap with the specified initial capacity and specified load factor.
Syntax
HashMap<K, V> hash = new HashMap<K, V>(int initialCapacity, int loadFactor);
You can also try this code with Online Java Compiler
Run Code
Example
import java.io.*;
import java.util.*;
class Main {
public static void main(String args[])
{
HashMap<Integer, String> map1 = new HashMap<Integer, String>(2,0.5f);
map1.put(1, "coding");
map1.put(2, "ninjas");
System.out.println("Mappings of HashMap map1 are : "+ map1);
}
}
You can also try this code with Online Java Compiler
Run Code
Output
Mappings of HashMap map1 are : {1=coding, 2=ninjas}
You can also try this code with Online Java Compiler
Run Code
HashMap(Map map)
It is used to initialize the hashmap by using the elements of the given Map object “map.”
Syntax
HashMap<K, V> hash = new HashMap<K, V>(Map map);
You can also try this code with Online Java Compiler
Run Code
Example
import java.io.*;
import java.util.*;
class Main {
public static void main(String args[])
{
HashMap<Integer, String> map1 = new HashMap<Integer, String>();
map1.put(1, "coding");
map1.put(2, "ninjas");
HashMap<Integer, String> map2= new HashMap<Integer, String>(map1);
System.out.println("Mappings of HashMap map1 are : "+ map1);
System.out.println("Mappings of HashMap map2 are : "+ map2);
}
}
You can also try this code with Online Java Compiler
Run Code
Output
Mappings of HashMap map1 are : {1=coding, 2=ninjas}
Mappings of HashMap map2 are : {1=coding, 2=ninjas}
You can also try this code with Online Java Compiler
Run Code
Practice by yourself on online java compiler.
Operations on Hashmap
Although there are many operations on a HashMap today, we are going to discuss only the most commonly used functions
put()
put() method can add an element to the map. Also, we can change the element using the put() method.
remove()
We can use the remove() method to remove an element from the hashmap. This method takes the key as an input parameter and removes the mapping for a key from the given map if it exists.
get()
We can use the get() method to access an element from the hashmap for a corresponding key value.
clear()
We can use the clear() method to remove all the items from the Hashmap.
size()
we can use the size() method to get the total items present in the hashmap.
keySet()
We can use the keyset() method to iterate over the hashmap keys.
values()
We can use the values() method to iterate over the hashmap values.
Now let us demonstrate the above functions using an example
Example
import java.io.*;
import java.util.*;
class Main {
public static void main(String args[])
{
HashMap<Integer, String> map1 = new HashMap<Integer, String>();
//put()
map1.put(1, "Ninja");
map1.put(2, "Coder");
map1.put(3, "Happy");
map1.put(4, "Coding");
System.out.println("Mappings of HashMap map1 are : "+ map1);
//get()
System.out.println("Value with key 2 is "+map1.get(2));
//remove
map1.remove(2);
System.out.println("Mappings of HashMap map1 after removal are : "+ map1);
//size
System.out.println("Size of map1 are "+map1.size());
//keyset()
for (int i : map1.keySet()) {
System.out.println(i);
}
//values()
for (String i : map1.values()) {
System.out.println(i);
}
//clear()
map1.clear();
System.out.println("Mappings of HashMap map1 after clear are : "+ map1);
}
}
You can also try this code with Online Java Compiler
Run Code
Output
Mappings of HashMap map1 are : {1=Ninja, 2=Coder, 3=Happy, 4=Coding}
Value with key 2 is Coder
Mappings of HashMap map1 after removal are: {1=Ninja, 3=Happy, 4=Coding}
Size of map1 are 3
1
3
4
Ninja
Happy
Coding
Mappings of HashMap map1 after clear are : {}
You can also check out Internal Working of HashMap
Frequently Asked Questions
-
What is the Load factor in Hashmap?
The Load Factor is a factor used internally by HashMap to decide whether the size of the Bucket array has to be raised. It is 0.75 by default. When the number of nodes in the HashMap exceeds 75% of its total capacity, HashMap expands the size of its bucket array. When HashMap's bucket array size has to be extended, its capacity is always doubled.
-
What will happen if we store duplicate values in the HashMap?
If we try to store a key already present in the HashMap, it will override the old value associated with the key and replace it with the new value.
-
Can we store a null key in Hashmap?
Yes, a null key can be stored in hashmap, but only one null key is allowed at a time.
Key Takeaway
Today, we learned about hashmap in Java, its implementation, and its functionality and methods. We also learned different approaches to declare and initialize Hashmap.
Recommended Readings:
If you wish to learn more about Java from experts, you can check out the Basics of Java with Data Structures and Algorithms course by coding ninjas.
Happy Coding!