Introduction
In this article, we are going to describe the HasMap Class's functionality in Java. The HashMap is also part of the collection framework. And this class is in the java.util package of Java. The HashMap class uses a hash table to implement the Map interface. This allows the execution time of the basic operations, such as to get() and put(), to remain constant even for large sets.
The following figure shows where and for what use and the benefits of a hash map.
What is Hashing
Hashing means using a function or algorithm to map object data to some representative integer value. This so-called hash code (or simply hash) can then be used as a way to narrow down our search when looking for the item in the map.
Constructor Details
- HashMap( )
- HashMap(Map m)
- HashMap(int capacity)
- HashMap(int capacity, float fillRatio)
The first form constructs a default hash map. And the initial capacity is 16 and the default load factor (0.75). The second form initializes the hash map by using the elements of m. The third form initializes the capacity of the hash map to capacity but default load factor. The fourth form initializes both the capacity and fills ratio of the hash map by using its arguments. The meaning of capacity and fill ratio is the same as for HashSet, described earlier.
How you Iterate the value of HasMap?
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.Iterator;
- public class HashMapDemo
- {
- public static void main(String[] args)
- {
- HashMap < String, String > hMp = new HashMap < String, String > ();
- hMp.put("1", "Abhishek");
- hMp.put("2", "vineet");
- hMp.put("3", "Akshay");
- Collection c = hMp.values();
- Iterator itr = c.iterator();
- while (itr.hasNext())
- {
- System.out.println(itr.next());
- }
- }
- }

Some Operation with HashMap



Join the conversation! Your thoughts help the community grow.