Wednesday, April 1, 2009

Map-Hashtable-HashMap-WeakHashMap-IdentityHashMap

MAP Interface:A Map is an object that maps keys to values. Maps cannot contain duplicate keys: Each key can map to at most one value.

  • Map is not the child interface of Collection interface
  • If you want to store the values or objects as key value pairs we go for the the Map interface
  • Both key and values are objects
  • Duplication of keys is not allowed but values may be duplicated
  • Key –Value Pair is called an Entry



Methods in Map Interface:

1)Object put(Object key,Object value) //Inserts a value into the map.if the key is already exists it will replace the old values with new one
2)Object get(Object key) // Returns the value associated with the key otherwise returns null
3)Object remove(Object key) // It removes the entry associated with the key and returns the corresponding value otherwise returns null
4)boolean containsKey(Object key)
5)boolean containsValue(Object key)
6)int size()
7)boolean isEmpty()
8)void clean()
9)void putAll(Map m)
10)Set keySet()
11)Collection values()
12)Set emptySet()
// The following three methods collection view of Map
13)Object getKey();
14)Object getValue();
15)Object setValue(Object obj)
//Entry is an inner interface present inside the map.It contains
the following three methods which can be applied on the entry object.
16)Object getKey();
17)Object getValue();
18)Object setValue(Object obj)

Note:
interface Map{
Interface Entry{ // inner interface entry
Object getKey();
Object getValue();
Object setValue(Object obj)
}
}

Hashtable:
  • The underlying datastructure for the HashTable is the Hastable itself.
  • Heterogeneous values are allowed for both keys and values
  • null insertion is allowed for both keys and values for the first element violation leads to NullPointerException
  • Almost all methods or Hashtable are synchronized hence it is thread safe
  • Insertion order is not preserved and the objects are arranged based on hashcode
  • Duplicate objects for values but keys are not to be duplicated
Hashtable Constructors:
Hashtable table=new Hashtable();
Hashtable table=new Hashtable(int initialCapacity);
Hashtable table=new Hashtable(int initialCapacity,float fillRatio);
Hashtable table=new Hashtable(Map m);

Hashtable Demo:
import java.util.Hashtable;

public class HashtableEx1 {
public static void main(String[] args) {
Hashtable table=new Hashtable();
table.put(new Integer(1),"xxx");
table.put(new Integer(24),"yyy");
table.put(new Integer(3),"zzz");
table.put(new Integer(8),"aaa");
table.put(new Integer(9),"bbb");
table.put(new Integer(2),"sss");
System.out.println("The Hashtable is "+table);
}
}

output:
The Hashtable is {9=bbb, 8=aaa, 3=zzz, 2=sss, 24=yyy, 1=xxx}


HashMap:
  • The underlying datastructure for HashMap is Hashtable
  • Duplicate keys are not allowed but values may be duplicated
  • Insertion order is not preserved
  • Heterogeneous key and values are allowed
  • Null key is allowed only once but values are nulls for any number of times
HashMap Constructors:
1) HashMap map=new HashMap();
it Create the empty hashmap with the default initial capacity 16 and fillRatio 0.75
2) HashMap map=new HashMap(int initialCapacity);
it Create a HashMap with the specified nitialCapacity and default load factor
3) HashMap map=new HashMap(int nitialCapacity,float loadFactor);
4) HashMap map=new HashMap(Map m);

HashMapDemo Program:
import java.util.*;
public class HashMapEx1 {
public static void main(String... args) {
HashMap map=new HashMap();
map.put("orange",new Integer(1000));
map.put("apple",new Integer(2000));
map.put("banana",new Integer(3000));
map.put("grapes",new Integer(4000));
System.out.println("The Map "+map);
System.out.println(map.put(("orange"),new Integer(1001)));
System.out.println("map "+map);
Set s=map.keySet();
System.out.println("The Key Set"+s);
Collection values=map.values();
System.out.println("The Values Are "+values);
Set s1=map.entrySet();
System.out.println("The Entry Set"+s1);
}
}


output:
The Map {orange=1000, grapes=4000, apple=2000, banana=3000}
1000
map {orange=1001, grapes=4000, apple=2000, banana=3000}
The Key Set[orange, grapes, apple, banana]
The Values Are [1001, 4000, 2000, 3000]
The Entry Set[orange=1001, grapes=4000, apple=2000, banana=3000]

LinkedHashMap:
It is exactly similar to the HashMap except the following differences
HashMap:
  • Underlying datastructure is Hashtable
  • Insertion order is not preserved
  • While iterating we can not give guarantee for processing order. Hence we can not use it for caching
LinedHashMap:
  • Underlying datastructures are hashtable and doubly linkedlist
  • Insertion order of elements is preserved
  • While iterating elements we can give guarantee for processing order.Hence we can use for caching
IdentityHashMap:
In case of HashMap JVM uses the equals() method to identify the duplicate keys
But if want to use the == operator to identify the duplicates we go for the IdentityHashMap
Incase of IdentityHashMap two key reference i1 and i2 are equal if and only if bot i1 and i2 are pointing to the same object on the heap

IdentityHashMap Demo Program:
import java.util.IdentityHashMap;

public class IdentityHashMapEx1 {
public static void main(String... args) {
IdentityHashMap map=new IdentityHashMap ();
Integer i1=new Integer(10);
Integer i2=new Integer(10);
map.put(i1,"orange");
map.put(i2,"apple");
System.out.println("The Map"+map);
}
}


WeakHashMap:
  • WeakHashMap is not a child class of HashMap
  • HashMap dominates the garbage collector.if any objects are associated with the HashMap eventhough that object does not have any external references .Garbage collector is not allowed to destroy that object
  • But garbage collector dominate the WeakHashMap that is in case of hashmap if the key is not reachable garbage collector is allowed to destroy whole entry associated with the key
WeakHashMap Demo Program:
import java.util.WeakHashMap;
class Temp {
public void finalize() {
System.out.println("finalize() Called");
}
public String toString() {
return "Temp";
}
}
public class WeakHashMapEx1 {
public static void main(String[] args) {
WeakHashMap map=new WeakHashMap();
Temp t=new Temp();
map.put(t,"orange");
t=null;
System.gc();
System.out.println("The Map"+map);
}
}

output:
finalize() Called
The Map{}

1 comments:

Anonymous said...

I Like it ...

Post a Comment