Monday, May 2, 2011

HashMap performance factor:initial capacity and load factor

An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the capacity is roughly doubled by calling the rehash method.
As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the lookup cost (reflected in most of the operations of the HashMap class, including get and put). The expected number of entries in the map and its load factor should be taken into account when setting its initial capacity, so as to minimize the number of rehash operations. If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur.

Implementation of correct hashcode



Scenario:
I have one class Employee That have some filed and Employee also implemented Hash code and equals method
Case 1: Hashcode () was implemented such that it always return some random no for every object.
Like for same object also returning different hascode
Case2: Hashcode () was implemented such way it return same hashcode for all object may me they object are same or different.
What is the output of following program?

public class HascodeTest {
           
            public String name;
           

            public static void main(String[] args) {
                        HashMap test=new HashMap();
                        HascodeTest ht=new HascodeTest("test");
                       
                       
                        test.put(ht,"hello");                 
                        test.put(ht,"hello");
                        test.put(ht,"hello");
                        test.put(ht,"hello");
                        test.put(ht,"hello");
                        System.out.println("HashmapSize"+test.size());
                        System.out.println(ht);
            }


public HascodeTest(String name) {
                        super();                       
                        this.name = name;
            }

            @Override
            public boolean equals(Object obj) {
                        // TODO Auto-generated method stub
                        return super.equals(obj);
                        //return true;
            }



            @Override
            public int hashCode() {
                        // TODO Auto-generated method stub
                        int hashcode=super.hashCode();
                        System.out.println("HashCode:"+hashcode);            
                        int renVal=(int) (31 * Math.random());
                        System.out.println("HashCode"+renVal);
                        return renVal;
                        //return 3  in case 1;
            }


            @Override
            public String toString() {
                        System.out.println("Calling to String ");
                        return this.name;
                       
            }         
}

Ans:
Case 1 :Size of hashmap is 4
Case2:size of hashmap is 1

First it checks if hashode are same then only he goes for equity check and key are equal or not 
i.e if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
So if hashocode are different then it has not check equals.
So implementation of correct hashcode is very important

           
           

static synchronized methods and non-static synchronized method

Q Can two threads call two different static synchronized methods of the same class?
Ans) No. The static synchronized methods of the same class always block each other as only one lock per class exists. So no two static synchronized methods can execute at the same time.
Q Does a static synchronized method block a non-static synchronized method?
Ans)No As the thread executing the static synchronized method holds a lock on the class and the thread executing the non-satic synchronized method holds the lock on the object on which the method has been called, these two locks are different and these threads do not block each other.