Link to home
Start Free TrialLog in
Avatar of sudhakar_koundinya
sudhakar_koundinya

asked on

Hashtable Vs Hashmap

Hello All,

I just wanted to know which is faster.

In terms of putting the key/value pair and also accessing them.

thanks
sudhakar
SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sudhakar_koundinya
sudhakar_koundinya

ASKER

ok let me test that
public final static Object[] getUrlList8(java.util.List urls) {
            //Hashtable hostToUrls = new Hashtable(urls.size());
            HashMap hostToUrls = new HashMap(urls.size());
            StringBuffer accountBuffer = new StringBuffer(32);
            for (int i = 0; i < urls.size(); i++) {
                  accountBuffer.setLength(0);
                  String url = (String) urls.get(i);
                  // System.err.println(url);
                  int forwardSlashCount = 0;
                  int hostHashCode = 0;

                  for (int ixChar = 4; forwardSlashCount < 5 && ixChar < url.length(); ixChar++) {
                        // for (int ixChar = 4; forwardSlashCount < 4 ; ixChar++) {

                        if (url.charAt(ixChar) == '/') {
                              forwardSlashCount++;
                        }
                        if (forwardSlashCount == 2) {
                              // Compute hashcode of host
                              hostHashCode = 3 * hostHashCode + url.charAt(ixChar);
                        } else if (forwardSlashCount == 4) {
                              accountBuffer.append(url.charAt(ixChar));
                        }
                  }
                  // end for

                  accountBuffer.append(url.charAt(4)).append(hostHashCode);
                  //   System.out.println(accountBuffer) ;
                  final String key = accountBuffer.toString();
                  ArrayList urlsForHost = (ArrayList) hostToUrls.get(key);
                  if (urlsForHost == null) {
                        urlsForHost = new ArrayList();
                        urlsForHost.add(url);
                        hostToUrls.put(key, urlsForHost);
                  } else {
                        urlsForHost.add(url);
                  }
            }
            // Enumeration e= hostToUrls.keys();
            //   while(e.hasMoreElements())
              //  System.err.println(e.nextElement());
            //return Collections.list(hostToUrls.values());
            return hostToUrls.values().toArray();
      }



public final static List getUrlList9(java.util.List urls) {
            //Hashtable hostToUrls = new Hashtable(urls.size());
            Hashtable hostToUrls = new Hashtable(urls.size());
            StringBuffer accountBuffer = new StringBuffer(32);
            for (int i = 0; i < urls.size(); i++) {
                  accountBuffer.setLength(0);
                  String url = (String) urls.get(i);
                  // System.err.println(url);
                  int forwardSlashCount = 0;
                  int hostHashCode = 0;

                  for (int ixChar = 4; forwardSlashCount < 5 && ixChar < url.length(); ixChar++) {
                        // for (int ixChar = 4; forwardSlashCount < 4 ; ixChar++) {

                        if (url.charAt(ixChar) == '/') {
                              forwardSlashCount++;
                        }
                        if (forwardSlashCount == 2) {
                              // Compute hashcode of host
                              hostHashCode = 3 * hostHashCode + url.charAt(ixChar);
                        } else if (forwardSlashCount == 4) {
                              accountBuffer.append(url.charAt(ixChar));
                        }
                  }
                  // end for

                  accountBuffer.append(url.charAt(4)).append(hostHashCode);
                  //   System.out.println(accountBuffer) ;
                  final String key = accountBuffer.toString();
                  ArrayList urlsForHost = (ArrayList) hostToUrls.get(key);
                  if (urlsForHost == null) {
                        urlsForHost = new ArrayList();
                        urlsForHost.add(url);
                        hostToUrls.put(key, urlsForHost);
                  } else {
                        urlsForHost.add(url);
                  }
            }
            // Enumeration e= hostToUrls.keys();
            //   while(e.hasMoreElements())
              //  System.err.println(e.nextElement());
            return Collections.list(hostToUrls.elements());
            
      }


When I tested  above two methods I got same results :-(

public static void main(String[] args) {
            // TODO code application logic here
            Vector vect = new Vector();
            ListArrayTest test = new ListArrayTest();
            vect = test.getURLS();
            java.util.Date d = new java.util.Date();
            //List l[]=test.getURLList(vect);
            Object l[] = test.getUrlList8(vect);
      //      List l = test.getUrlList9(vect);
            //for (int i = 0; i < l.size(); i++) {
                  for (int i = 0; i < l.length; i++) {
                  //System.out.println("List :" + i + " \t");

            //      List ll = (List) l.get(i);
                        List ll = (List) l[i];
                  // System.out.println("\t"+ll.get(0));
                  for (int j = 0; j < ll.size(); j++) {
                        //System.out.println("\t" + ll.get(j));
                  }
            }
            //List l[]=test.getURLList1(vect);
            //List l[]=test.getUrlList(vect);
            java.util.Date d1 = new java.util.Date();
            System.err.println(d1.getTime() - d.getTime());

      }

Here is the test case

public Vector getURLS() {
            Vector array=new Vector();
            for(int i=0;i <100;i++)
            {
                  for(int j=0;j<1000;j++)
                  {
                        array.add("http://Test"+i+"/exchange/"+(i+j));
                         
                  }
            }
      

> When I tested  above two methods I got same results :-(

Exactly the same?
yes


 one interesting case is when I tested the following code

import java.util.*;
public class HashTest
{
        public static void main(String s[])
      {
              int i=0;
            Date start=new Date();
            Hashtable ht=new Hashtable();
            for( i=0;i<100000;i++)
            {
               ht.put(new Integer(i),new Integer(i));
            }
            ht=null;
            Date end=new Date();
            System.err.println(end.getTime()-start.getTime());
            
             start=new Date();
             
            HashMap hm=new HashMap();
            for( i=0;i<100000;i++)
            {
               hm.put(new Integer(i),new Integer(i));
            }
            hm=null;
            end=new Date();
            System.err.println(end.getTime()-start.getTime());
            
      }
}


Hashtable Time for putting key value pairs :234
Hashmap Time for putting key value pairs :359

So Hashmap took more time although it is unsynchronized

I ran the above code ten times on Windows 2000 with 1.2GHz AMD Athlon with jdk1.4.0_03. The results are:

Hashtable: 611, 601, 611, 621, 610, 611, 621, 621, 611, 621
HashMap: 520, 511, 521, 541, 531, 531, 531, 531, 531, 521

Windows XP- P4 1.2 Ghz and Jdk 1.4

import java.util.*;
public class HashTest
{
        public static void main(String s[])
      {
           for(int k=0;k<10;k++)
           {
           int i=0;
            Date start=new Date();
            Hashtable ht=new Hashtable();
            for( i=0;i<100000;i++)
            {
               Integer ii=new Integer(i);
               ht.put(ii,ii);
            }
            ht=null;
            Date end=new Date();
            System.err.println("Hashtable Time for putting key value pairs :"+(end.getTime()-start.getTime()));
            
             start=new Date();
             
            HashMap hm=new HashMap();
            for( i=0;i<100000;i++)
            {
                  Integer ii=new Integer(i);
               hm.put(ii,ii);
            }
            hm=null;
            end=new Date();
            System.err.println("Hashmap Time for putting key value pairs :"+(end.getTime()-start.getTime()));
           }
            
      }
}



Hashtable Time for putting key value pairs :235
Hashmap Time for putting key value pairs :359
Hashtable Time for putting key value pairs :187
Hashmap Time for putting key value pairs :328
Hashtable Time for putting key value pairs :188
Hashmap Time for putting key value pairs :328
Hashtable Time for putting key value pairs :187
Hashmap Time for putting key value pairs :344
Hashtable Time for putting key value pairs :188
Hashmap Time for putting key value pairs :343
Hashtable Time for putting key value pairs :188
Hashmap Time for putting key value pairs :359
Hashtable Time for putting key value pairs :203
Hashmap Time for putting key value pairs :328
Hashtable Time for putting key value pairs :204
Hashmap Time for putting key value pairs :343
Hashtable Time for putting key value pairs :188
Hashmap Time for putting key value pairs :344
Hashtable Time for putting key value pairs :187
Hashmap Time for putting key value pairs :328
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's weird. Tests in the above links show that sometimes HashMap is faster and sometimes Hashtable is faster. I was under the impression that HashMap was always faster than Hashtable when synchronization is not an issue.
for the following code

the results are like this :-)

import java.util.*;
public class HashTest
{
        public static void main(String s[])
      {
           for(int k=0;k<10;k++)
           {
            new myThread(k).start();
           }
            
      }
}

class myThread extends Thread
{
      int kk=0;
      public myThread(int i)
      {
            this.kk=i;
      }
      public void run()
       {
             int i=0;
            Date start=new Date();
            Hashtable ht=new Hashtable();
            for( i=0;i<100000;i++)
            {
               Integer ii=new Integer(i);
               ht.put(ii,ii);
            }
            ht=null;
            Date end=new Date();
            System.err.println("Thread "+kk+" : Hashtable Time for putting key value pairs :"+(end.getTime()-start.getTime()));
            
             start=new Date();
             
            HashMap hm=new HashMap();
            for( i=0;i<100000;i++)
            {
                  Integer ii=new Integer(i);
               hm.put(ii,ii);
            }
            hm=null;
            end=new Date();
            System.err.println("Thread "+kk+" : HashMap Time for putting key value pairs :"+(end.getTime()-start.getTime()));
       }
}



Thread 0 : Hashtable Time for putting key value pairs :360
Thread 4 : Hashtable Time for putting key value pairs :250
Thread 8 : Hashtable Time for putting key value pairs :234
Thread 1 : Hashtable Time for putting key value pairs :954
Thread 3 : Hashtable Time for putting key value pairs :2032
Thread 0 : HashMap Time for putting key value pairs :1781
Thread 3 : HashMap Time for putting key value pairs :687
Thread 2 : Hashtable Time for putting key value pairs :2891
Thread 4 : HashMap Time for putting key value pairs :2250
Thread 1 : HashMap Time for putting key value pairs :2468
Thread 9 : Hashtable Time for putting key value pairs :703
Thread 2 : HashMap Time for putting key value pairs :875
Thread 6 : Hashtable Time for putting key value pairs :2937
Thread 6 : HashMap Time for putting key value pairs :516
Thread 9 : HashMap Time for putting key value pairs :797
Thread 8 : HashMap Time for putting key value pairs :3657
Thread 7 : Hashtable Time for putting key value pairs :3844
Thread 7 : HashMap Time for putting key value pairs :312
Thread 5 : Hashtable Time for putting key value pairs :250
Thread 5 : HashMap Time for putting key value pairs :359
I mean

Thread 0 : HashMap Time for putting key value pairs :1781
Thread 0 : Hashtable Time for putting key value pairs :360
Thread 1 : HashMap Time for putting key value pairs :2468
Thread 1 : Hashtable Time for putting key value pairs :954
Thread 2 : HashMap Time for putting key value pairs :875
Thread 2 : Hashtable Time for putting key value pairs :2891
Thread 3 : HashMap Time for putting key value pairs :687
Thread 3 : Hashtable Time for putting key value pairs :2032
Thread 4 : HashMap Time for putting key value pairs :2250
Thread 4 : Hashtable Time for putting key value pairs :250
Thread 5 : HashMap Time for putting key value pairs :359
Thread 5 : Hashtable Time for putting key value pairs :250
Thread 6 : HashMap Time for putting key value pairs :516
Thread 6 : Hashtable Time for putting key value pairs :2937
Thread 7 : HashMap Time for putting key value pairs :312
Thread 7 : Hashtable Time for putting key value pairs :3844
Thread 8 : HashMap Time for putting key value pairs :3657
Thread 8 : Hashtable Time for putting key value pairs :234
Thread 9 : HashMap Time for putting key value pairs :797
Thread 9 : Hashtable Time for putting key value pairs :703
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> for(int z=0;z<100000;z++){
    >>                                                      array[z] = new Integer(z + k + 1);
        >>                        }

I do not understand what is ur intention here
>what is ur intention here    
I wanted to make sure that the tables actually had to change key and values for each pass through loop.
>new Integer(z + k + 1)    
This way the tables have to change entries in each pass.  I tried many different ways.  But, maybe we still need to do some work to measure your original  query.
>In terms of putting the key/value pair and also accessing them.      
rrz
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That are good findings  :-)

After executing ur code, But still I am getting impression that, Hashtable is Fast.
Allocating array of objects (1) ...
Array of objects (1) allocated.
Hashtable put same objects : total = 31ms, each = 0.00031ms
HashMap put same objects : total = 32ms, each = 0.00032ms
Hashtable put different objects : total = 218ms, each = 0.00218ms
HashMap put different objects : total = 344ms, each = 0.00344ms
Hashtable get same existing objects : total = 31ms, each = 0.00031ms
HashMap get same existing objects : total = 16ms, each = 0.00016ms
Hashtable get different existing objects : total = 31ms, each = 0.00031ms
HashMap get different existing objects : total = 63ms, each = 0.00063ms
Allocating array of objects (2) ...
Array of objects (2) allocated.
Hashtable get same not existing objects : total = 16ms, each = 0.00016ms
HashMap get same not existing objects : total = 16ms, each = 0.00016ms
Hashtable get different not existing objects : total = 31ms, each = 0.00031ms
HashMap get different not existing objects : total = 47ms, each = 0.00047ms
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I Just gave B because no one of us know correct bench marking performance tests reagrding this.

I am sorry if I hurt you with B and above sentence

Regards
Sudhakar
So do we know for sure what's going on? I see from the test cases that sometimes HashMap is faster and some other times Hashtable is faster.
>> So do we know for sure what's going on? I see from the test cases that sometimes HashMap is faster and some other times Hashtable is faster.

The only explanation i think is that HashMap and Hashtable don't use the same processor instruction set in their native implementation. And these native code may be different (optimized) for each CPU type. On 1 processor, some instructions used by Hashtable are faster than those used by HashMap, and on another processor, instructions used by HashMap may be faster then those used by Hashtable.

In conclusion, we can't choose HashMap or Hashtable in order to have a faster code, if the application will run on different processor types.