Java collections Framework- HashMap Class

Java collections Framework- HashMap Class


Posted in : Java Posted on : December 2, 2010 at 5:26 PM Comments : [ 0 ]

HashMap class is part of java.util package. HashMap class can add key and value put(key, value) pair elements.

Java collections Framework- HashMap Class

HashMap class is part of java.util package. HashMap class can add key and value put(key, value) pair elements. This HashMap permits null key and value. But HashMap is unsynchronized. HashMap class gives no guarantee to return as original order as entered.HashMap extends AbstractMap class and implements Map interface. Key and value of HashMap can get by Set Interface and Map interface through Iterator 
interface.
An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased.
If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. 
This is typically accomplished by synchronizing on some object that naturally encapsulates the map.

The HashMap class supports four constructors.

HashMap( ) - This constructor constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).

HashMap(int initialCapacity) - This constructor constructs an empty HashMap with the specified initial capacity.

HashMap(int initialCapacity, float loadFactor) - This constructor constructs an empty HashMap with the specified initial capacity and load factor.

HashMap(Map m) - This constructor constructs a new HashMap with the same mappings as the specified Map.

The list of methods supported by Hash Map Class are shown in the table given below:

put( ) -          Associates the specified value with the specified key in this map. 

clear( ) -       Removes all mappings from the map.

putAll( ) -     Copies all of the mappings from the specified map to the map.

isEmpty( ) -  Returns true if this map contains no key-value mappings. 

iterator( ) -   Returns an Iterator object for the collection which may be used to retrieve an object. 

remove( ) -   Removes the mapping for this key from this map if it is present. 

keySet( ) -    Returns a set view of the keys contained in this map. 

entrySet( ) - Returns a collection view of the mappings contained in this map. 

values( ) -    Returns a collection view of the values contained in this map. 

size( ) -        Returns the number of key-value mappings in this map.

Here is the  example for demonstration of the map interface-

Example:- HashMapDemo.java

package devmanuals.com;

import java.util.*;

public class HashMapDemo {
	public static void main(String args[]) {
		HashMap hm = new HashMap();
		hm.put(1, "Gyan");
		hm.put(2, "Ankit");
		hm.put(3, "Arun");
		hm.put(4, "Anand");
		hm.put(5, "Ram");

		Set set = hm.entrySet();
		Iterator i = set.iterator();
		while (i.hasNext()) {
			Map.Entry me = (Map.Entry) i.next();
			System.out.print(me.getKey() + ": ");
			System.out.println(me.getValue());
		}
		System.out.println();

	}
}
Output:-
1: Gyan
2: Ankit
3: Arun
4: Anand
5: Ram

Download This Code.

Here is the Example of methods implementation of HashMap class.

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics