October 10, 2012

How to work 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.



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 hMp = new HashMap();

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:



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:



No comments:

Post a Comment

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts