Posts

Showing posts with the label map

WeakHashMap by example

Usually we read following explanation about java.util.WeakHashMap “A hashtable-based Map implementation with weak keys. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use” Unlike java.util.HashMap, WeakHashMap maintain weak reference for the all keys objects and strong reference of value objects. As per features of weak reference, they are more prone to be collection by GC. WeakHashMap runs some kind of unknown services to remove all the entries from collection whose keys are already garbage collected. It means entries in weak hash map may changes over the time. Whenever there  is a need of more memory to your application, GC cycle will execute and its job is to collect all un-referenced objects or weak references. In some cases GC may collected keys of weak hash map as those are a weak references. Once keys are garbage collected , WeakHashMap’s implementation job is to clear all such entries.   See the following program, I...

Map maintaining insert order of key-value pairs

Many time we want to maintain order of entries as it is inserted. HashMap does not satisfy this requirement. For all such requirement java collection framework has provided LinkedHashMap. Following example explains : 1: import java.util.*; 2: class MapTest 3: { 4: public static void main(String[] args) 5: { 6: System.out.println(" Using HashMap: "); 7: Map hashMap = new HashMap(); 8: hashMap.put(" One "," One "); 9: hashMap.put(" Two "," Two "); 10: hashMap.put(" Three "," Three "); 11: hashMap.put(" Four "," Four "); 12: hashMap.put(" Five "," Five "); 13: System.out.println(" Complete Map content : "+ hashMap.toString()); 14: System.out.println(" Iterating Key-Value: "); 15: Iterator itr = hashMap.keySet().iterator(); 16: while (itr.hasNext()){ 17: System.out.println(hashMap.get(itr.next()));...

HashMap hashCode collision by example

Image
As we all know Hash is a part of Java Collection framework and stores key-value pairs. HashMap uses hash Code value of key object to locate their possible in under line collection data structure, to be specific it is nothing but array. Hash code value of key object decide index of array where value object get stored. As per hashcode – equals method implementation rules Objects that are equal according to the equals method must return the same hashCode value. & If two objects are not equal according to equals, they are not required to return different hashCode values. As per above statement it is possible that two different object may have same hashcode values, this is called hashcode collision . To overcome this problem concept of bucket has been introduced. All the Value objects whose corresponding Key’s hashcode values are same , they fall under same bucket. Above diagram explains hash code collision. There are three key-value entries are shown , out of which second and thi...

Understanding Java Collection Framework

Image
Thought of sharing my understanding about utilizing java collection classes , so here is the post History : Vector & HashTable are introduced very first in JDK 1.0 and then complete collection framework is introduced in JDK 1.2. You can see complete hierarchy of collection framework here . Here i am going to explain their uses in short