What is Map in Java and there different implementations e.g. hashmap in Java

Map is common data structure and an important part of application programming , different language supports map in different way in this article we will see how Java supports Maps. For beginners Map is data structure which is used to put data as key value pair for quick retrieval. Map is derived from hashing technique and offers speedy retrieval of stored location known as bucket and based on efficiency of hashing technique can match array with consistent time for retrieval operation. Map allows two operations put and get, put is used to keep value into Map and get is used to take value from HashMap, sometime collision might happen when hashing technique finds same bucket position for various key elements. To resolve collision a linked list is kept in bucket are and every node of linked list stores data that is built of both value and key objects. Java Software is one of the preferred language because of its various capabilities such as platform independence, security and object oriented technique but except these main capability what java provides is full in built library for various mobile programming sought for and one of them are java collection API which provides HashMap, ArrayList, LinkedList and various different collection classes. In Java Collection is used to indicate those classes which store or store other objects. Java store its collection classes in java.util package and provides one interface called Map. Map describes basic possible operation in any kind of Map implementation for example get and put operation. In Java a map don't store any duplicate keys and every key can map to at most one value. Java supply many implementation of Map interface and those implementations are also present in java.util package. Some of most commonly Map implementations are IdentityHashMap, HashMap, and LinkedHashMap and HashMap in Java is the most popular of them after ArrayList in Java. These collection types are based on many data structure e.g. LinkedList, Array and Map. In this article we will discuss different collection classes based on HashMap data structure and Map interface.


The order of the map is the order in which the Iterator of the map's collection views returns their constituents. Iterator is an object which is used to retrieve objects from Collection within a loop. Some Map examples like the TreeMap maintains there order since they are derived from SortedMap and others, like HashMap doesn't maintains any order. To see more on How HashMap works in Java check on link. It’s imperative to understand that key object should implement equals and hashcode methods correctly in Java and caution great care must exercise if mutable elements are used as Map keys. Behavior of a map is uncertain if the value of an element is altered in a way which impact equals comparisons while the object is a key in map. IdentityHashMap implements Map interface with a hash table, uses reference-equality in place of object-equality while comparing keys (and values).


Couple of Map implementations also has restrictions on the keys and values they contain. For example some implementation doesn't keep null values and null keys and few have restrictions on the class of there keys. Insertion of wrong key or value throws unchecked exception , typically NullPointerException or ClassCastException for example TreeMap can't permit classes other than Type of there keys it intends you cannot put String and Double in TreeMap since they get compare to one another to allow sorted order and shall throw ClassCastException while comparing String and other class. Another important type which is based upon hashing and gives Map kind of functionality is Hashtable. Hashtable is present in java from JDK1.0 and don't extends Map interface rather than it implements Dictionary interface. Hashtable gives same functionality as HashMap but varies from HashMap in little respect you can look http://javarevisited.blogspot.com/2010/10/difference-between-hashmap-and.html here.

More by this Author


Comments 1 comment

java67 4 years ago

See here for difference between ConcurrentHashMap and HashMap in Java : http://java67.blogspot.sg/2012/08/difference-betwe...

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working