Wednesday, October 16, 2013

HashMap important methods... notes ...

HashMap:

java.util.Map is an Interface

public interface java.util.Map{
    public abstract int size();
    public abstract boolean isEmpty();
    public abstract boolean containsKey(java.lang.Object);
    public abstract boolean containsValue(java.lang.Object);
    public abstract java.lang.Object get(java.lang.Object);
    public abstract java.lang.Object put(java.lang.Object, java.lang.Object);
    public abstract java.lang.Object remove(java.lang.Object);
    public abstract void putAll(java.util.Map);
    public abstract void clear();
    public abstract java.util.Set keySet();
    public abstract java.util.Collection values();
    public abstract java.util.Set entrySet();
    public abstract boolean equals(java.lang.Object);
    public abstract int hashCode();
}


java.util.HashMap is an implementation class to Map interface

public class java.util.HashMap extends java.util.AbstractMap implements java.util.Map,java.lang.Cloneable,java.io.Serializable{
    static final int DEFAULT_INITIAL_CAPACITY;
    static final int MAXIMUM_CAPACITY;
    static final float DEFAULT_LOAD_FACTOR;
    transient java.util.HashMap$Entry[] table;
    transient int size;
    int threshold;
    final float loadFactor;
    volatile transient int modCount;
    public java.util.HashMap(int, float);
    public java.util.HashMap(int);
    public java.util.HashMap();
    public java.util.HashMap(java.util.Map);
    void init();
    static int hash(int);
    static int indexFor(int, int);
    public int size();
    public boolean isEmpty();
    public java.lang.Object get(java.lang.Object);
    public boolean containsKey(java.lang.Object);
    final java.util.HashMap$Entry getEntry(java.lang.Object);
    public java.lang.Object put(java.lang.Object, java.lang.Object);
    void resize(int);
    void transfer(java.util.HashMap$Entry[]);
    public void putAll(java.util.Map);
    public java.lang.Object remove(java.lang.Object);
    final java.util.HashMap$Entry removeEntryForKey(java.lang.Object);
    final java.util.HashMap$Entry removeMapping(java.lang.Object);
    public void clear();
    public boolean containsValue(java.lang.Object);
    public java.lang.Object clone();
    void addEntry(int, java.lang.Object, java.lang.Object, int);
    void createEntry(int, java.lang.Object, java.lang.Object, int);
    java.util.Iterator newKeyIterator();
    java.util.Iterator newValueIterator();
    java.util.Iterator newEntryIterator();
    public java.util.Set keySet();
    public java.util.Collection values();
    public java.util.Set entrySet();
    int capacity();
    float loadFactor();
}


The above code is generated from the command 'javap'  
usage: javap java.util.Map
           javap java.util.HashMap

From the above methods, some methods are very important methods we have to consider.

in Map:

1) public java.lang.Object put(java.lang.Object, java.lang.Object);
 
-  put method is to put an element/ object / value  in the map object with a reference/map to it (key).
-  Here key, values can be any type.
-  Most preferably key must be immutable and value can be any. Because HashMap concept is depending on hashcode / hashing mechanism. If the key is mutable then the hashcode will be changed, So, It may leads to unreliable. see hashing and bucketing concept  for more clarity.

          This put method returns an object only when try to put an object in the place of old object, and this old object will be returned. From this we will be notified that the returned object is replaced by new object.

      For example: 
m.put("1","one")      --->    returns null   ( because no object (value) was there with that key)
m.put("1","ONE")    --->   returns "one" (because "one" was the old object)


2) public java.lang.Object remove(java.lang.Object); 

- remove removes an element/ value from the map object, here we have to say map that remove the element which is referenced/mapped with this key. So we have to give the corresponding key to remove an element/ value from map object.
      
       remove(key) : returns value of that key
        This remove method returns an Object that is the mapping value you specified key to remove from map object otherwise returns null if the specified key is not there in map object. From this we will be notified that the specified key is removed along with value.

    For example: 
m.put("1", "one")    ----> returns null (because no object (value) was there with that key)
m.remove("1")        ----> returns "one" (because "one" is there with that key)
m.remove("1")        ----> returns null (because no object (value) is there with that key)


3) public java.util.Set keySet();

     This keySet method returns java.util.Set implemented class Object ( We have unknown collection implementations ) , So we have to consider this object as Set type object.

  -  An unordered Set object.
  -  we can iterate this set object and get values from map object.
  -  we can make this collection object to an array object.


    For example:
       1)    Set set = m.keySet();
               Iterator itr = set.iterator();

while (itr.hasNext()) {
System.out.println(m.get(itr.next()));
}//end while  
o/p ...
three
two
one
         2)      Object[] oar= set.toArray();
                  for (int i = 0; i < oar.length; i++) {
System.out.println((String)oar[i]);
}//
o/p...
3
1

     In this second example you cannot convert the object oar to String array object directly like this
String[] strAr=(String[]) oar;  ----- X    
          Because the elements of Object[] array are to be type casted to String type at runtime. 

4) public java.util.Collection values();

      This values method returns all values in the map object in Collection form / notation /view  that can be of any collection interface implementation class object type.

-  An unordered Collection object.
-  we can iterate this Collection object and use values of map object.
-  we can make this collection object to an array object.

  see keySet method example .

5) public java.util.Set entrySet(); 

     Set<Map.Entry<K,V>>  entrySet() 

  This entrySet method returns Set object which contains Entry(is a inner class from Map interface) class objects. These Entry objects are the main objects which holds key, value pairs.


look into Entry api 


in HashMap:

1) public java.lang.Object clone();

 -   Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.

     This clone method returns a copy of the map object that will have all the elements as old in it.

for example:

           map.remove("2");
           HashMap hMap=(HashMap)map;
           Map bMap =(HashMap)hMap.clone();
                                System.out.println(bMap.isEmpty()); -- > false
                                System.out.println(bMap.size()); --> some value
                                System.out.println(bMap.containsKey("1")); -- >true
                                System.out.println(bMap.containsValue("two"));-->false
                                System.out.println("TWO".equalsIgnoreCase(bMap.get("two"))); --> false



Key Points....

- Hashmap allows duplicate keys and duplicate values including null also.

for example:
                                       Map mp=new HashMap();
                                        mp.put(null,null);
                                        mp.put(null,null);
                                        mp.put("1",null);
                                        mp.put("2","ttt");
                                System.out.println(mp.get("1"));
                                        mp.put(null,"33");
                                System.out.println(mp.put("1","one"));
                                System.out.println(mp.put("2","two"));
                                System.out.println(mp.get("1"));
o/p:
null
null
ttt
one

- This implementation (HashMap) is not synchronized.
 If we need HashMap to be synchronized, we have to approach 
   Map m = Collections.synchronizedMap(new HashMap(...));

- Initially this HashMap object contains null key, null value...and we can insert null key and null values later also. 

for example:
                                        Map mp=new HashMap();
                                        System.out.println(mp.put(null,null)); --- > returns null

and also we can insert one more null later..


Hashtable imp methods



to be continued......








No comments:

Post a Comment