Java HashMap

By: Rahim Vindhani

Hashmap

A collection allows a group of objects to be treated as a single unit. Map is one of the core interfaces of java collection framework that defines operations for maintaining mappings of keys to values.

Map interface does not implement Collection interface, because it does not contain elements but contains entries of keys and their corresponding values (i.e. called mapping).

Map does not allow duplicate keys. So there is utmost one value that is mapped with the given key. Both key and value must be an Object (Primitive values must be wrapped).

HashMaps will automatically grow when you add too many elements. However, growing requires copying, rehashing and rechaining, which affects its overall performance.

Performance of HashMap depends on two important factors that are 
Initial Capacity and
Load Factor

Initial Capacity is the capacity at the time the HashMap is created. Load factor determines when to increase the capacity of the HashMap. The default load factor is 0.75.

Important Note:

The initial capacity is not the actual number of elements you plan to store in HashMap. Say for example, if you set initial capacity of 100 and the load factor is 0.75, then the capacity of HashMap will be automatically increased when it reaches to 75 not 100.

Note : As opposed to Hashtable, HashMap class permits null values and null keys. In addition to this HashMap is also not synchronized as Hashtable.

If needed it should be synchronized externally using

Map myMap = Collections.synchronizedMap (new HashMap ());

This implementation of HashMap provides constant-time performance for the basic operations like get and put.

Constructors
HashMap ()
Constructs empty HashMap with a default initial capacity 11 and load factor 0.75.

HashMap (int initialCapacity)
Constructs empty HashMap with the specified initial capacity and default load factor 0.75.

HashMap (int initialCapacity, float loadFactor)
Constructs empty HashMap with the specified initial capacity and the specified load factor.

HashMap (Map t)
Constructs a new HashMap with the mappings same as the passed Map.

Basic methods
Object get (Object key)
Returns the value mapped to the specified key, or null if no entry is found.

Object put (Object key, Object value)
Maps the specified key to the specified value in this HashMap and returns the value previously associated with the specified key, if any. Otherwise, it returns the null value.

Object remove (Object key)
Removes the key and its associated value from the HashMap, and returns the value previously associated with the specified key, if any. Otherwise, it returns the null value.

boolean containsKey(Object key)
Returns true if the specified key is mapped to some value in the map, otherwise false.

boolean containsValue(Object value)
Returns true if there are one or more keys mapped to the specified value, otherwise false.

int size()
Returns the size of the HashMap.

boolean isEmpty()
Returns true if HashMap is empty, otherwise false.

Other methods
void putAll(Map t)
Copies all mappings from the map to current HashMap and replaces existing entries, if any.

void clear()
Removes all mappings from HashMap.

Collection values()
Returns collection of the values contained in the HashMap.

Set entrySet()
Returns a Set of entries contained in the HashMap.

Set keySet()
Returns a Set of keys contained in the HashMap.

Object clone()
Creates copy of the HashMap.

Difference between Hashtable and HashMap:

Hashtable does not permit null keys while HashMap allows both null keys and values.

Hashtable is synchronized, while HashMap is not. If we need to synchronize it, we have to do it externally.

HashMaps work with Iterators where the older Hashtables work with Enumerations

As because HashMap is not synchronized, it is faster than hashtable.

Hashtables work best with capacities that are prime numbers. HashMaps round capacities up to powers of two.

Example of HashMap

import java.util.HashMap;

public class HashMapExample{

public static void main(String[] args) {

HashMap myMap = new HashMap();
map.put("1", "A");
map.put("2", "B");
map.put("3"Feature Articles, "C");
String one = (String) map.get("1");
System.out.println(one);

}

}

Top Searches on
Software
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 

» More on Software