Working With HashMap Class in Java


Introduction

In this article we are going to describe the HasMap Class's  functionality in Java. The HashMap is also the 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 get() and put(), to remain constant even for large sets.
A Map is a storage that holds key to value mappings. There cannot be duplicate keys in a Map and each key maps to at most one value. HashMap is unsynchronized and Hashtable is synchronized. HashMap is the faster one of the two. If you need to maintain map keys in an ordered fashion, you can use TreeMap.

The following figure shows where and for what use and the benifits of a hash map.

HashMap1.gif

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 fill 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

Example

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());

}

}

}

Output

hashmap1.jpg

Some Operation with HashMap

Example

import java.util.Hashtable;

public class HashMapOperation

{

public static void main(String[] s)

{

Hashtable table = new Hashtable();

table.put("k1", "abhishek");

table.put("k2", "vineet");

table.put("k3", "akshay");

System.out.println("Size of HashMap after addition : " + table.size());

Object obj = table.remove("k2");

System.out.println("Size of HashMap after remove one element : " + table.size());

System.out.println(obj + " Removed from HashMap");

System.out.println(" Is exist "+table.containsKey("k3"));

table.clear();

System.out.println("after remove all element the table is"+table);

}

}

Output

 hashmap3.jpg

Copy One hash table to another

Example

import java.util.Hashtable;

public class CopyDemo

{

public static void main(String[] s)

{

Hashtable table1 = new Hashtable();

table1.put("k1", "abhishek");

table1.put("k2", "vineet");

table1.put("k3", "deepak");

System.out.println(" first table is \n"+table1+"\n");

Hashtable table2 = new Hashtable();

table2.put("k4", "amit");

table2.put("k5", "anuj");

table2.put("k6", "omji");

System.out.println("Second table is \n"+table2+"\n");

table2.putAll(table1);

System.out.println("after copy the second first table with in second \n");

System.out.println(table2);

}

}

Output

 hashmap2.jpg

Resources

How to use FileWriter and FileReaderClass in JAVA
Abstract Class in JAVA
Standalone JAVA Class using Apache HTTP Client
How to Use the StringTokenizer Class in JAVA
Working with the BigDecimal class in JAVA

Up Next
    Ebook Download
    View all
    Learn
    View all