Monday, April 25, 2011

Looking on Map datastrucutre in Java


A Map is as data structure which is used to store data in form of key value pair for easy retrieval. Map is based on hashing technique and provides fastest access to stored location called bucket and based on efficiency of hashing algorithm can match array with constant time for get operation. In Map we used to operation put and get, put is used to store value into Map and get is used to retrieve value from HashMap, sometime collision may occur when hashing algorithm returns same bucket location for two different key objects. To resolve collision a linked list is stored in bucket location and each node of linked list contains data tuple which is made of both key and value objects.
Java is one of the widely used application programming language because of its various features such as platform independence, security and object oriented capability but other than these main capability what jav provides is extensive in built library for various application programming need and one of them are java collection API which provides HashMap, ArrayList, LinkedList and various other collection classes. In Java Collection is used to refer those classes which hold or contains other objects. These collection classes are based on various data structure e.g., Array, LinkedList and Map. In this article we will discuss various collection classes based on Map interface and HashMap data structure.Java also  keeps its collection classes in java.util package and gives one interface called Map. Map provides common possible operation in any sort of Map class for example get and put operation. In Java a map don't  store  any duplicate keys and each key can map to only one value. Java supply couple of implementation of Map interface and these implementations are also present in java.util package. Some of most commonly Map implementations are IdentityHashMap, HashMap, and LinkedHashMap .
The order of the map is the order on which the Iterator of the map's collection views gives their elements. Iterators are an object which is used to get  elements from Collection on a loop. Some Map exmaples like the TreeMap maintains there order since they are derived from SortedMap and others, like HashMap doesn't preserves any order. To see more about How HashMap works in Java look on link.It's important to understand that key object has to implement equals and hashcode contract as per specification in Java and caution great care must exercise if mutable elements  are used as Map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map. IdentityHashMap implements the Map interface with a hash table, using reference-equality in place of object-equality when comparing keys (and values).
Couple of Map implementations also have restrictions on the keys and values they store.For instance few implementation don't keep  null keys and null values and couple of has  limitation on the class of there keys. trying to insert  incorrect key or value throws un Runtime exception , typically NullPointerException or ClassCastException for instance TreeMap can't permit classes different than Type of there keys which  intends you cannot store  String and Float in TreeMap since they get compare to each other to keep sorted order and will throw ClassCastException while comparing String and different class. Another useful  type that is based on hashing and provides Map type of functionality is Hashtable. Hashtable is available in java from JDK1.0 and can't  extends Map interface rather than it implements Dictionary interface. Hashtable allows similar feature as HashMap but varies from HashMap in few respect you can see differences among HashMap and Hashtable here.


No comments:

Post a Comment