Java Collections – HashMap vs Hashtable vs TreeMap Performance

For this test, I decided to evaluate HashMap. Hashtable and vs TreeMap using the three basic operations (put(), get(), and remove()) and see which one is fastest once and for all. From the tests I performed, it appears that HashMap is the clear winner in all operations as was expected. I was surprised by the test case with Hashtable and HashMap when 10,000,000 objects were created.

Evaluating performance

  • #1. HashMap
  • #2. Hashtable
  • #3. TreeMap

Benchmark Number of Iterations/Sec

MapPerformance

Full Program Listing

package com.avaldes.tutorials;

import java.util.Hashtable;
import java.util.Map;
import java.util.HashMap;
import java.util.TreeMap;

public class MapPerformanceBenchmark {

  public static void testIterations(Map<Integer, Object> map, int length) {
    Object obj = new Object();
    for (int i=0; i<length; i++) map.put(i, obj);
    long startTime = System.currentTimeMillis();
    int iterations = 0;
    while (System.currentTimeMillis()-startTime < 1000) {
      iterations++;
      map.put(length+iterations, obj);
      map.get(length+iterations);
      map.remove(length+iterations);
    }
    System.out.println(map.getClass()+ " (" + length 
                            + "), iterations: " + iterations);
  }
  
  public static void main(String[] args) {
    
    testIterations(new Hashtable(), 1000);
    testIterations(new Hashtable(), 10000);
    testIterations(new Hashtable(), 100000);
    testIterations(new Hashtable(), 1000000);
    testIterations(new Hashtable(), 10000000);
    
    testIterations(new HashMap(), 1000);
    testIterations(new HashMap(), 10000);
    testIterations(new HashMap(), 100000);
    testIterations(new HashMap(), 1000000);
    testIterations(new HashMap(), 10000000);

    testIterations(new TreeMap(), 1000);
    testIterations(new TreeMap(), 10000);
    testIterations(new TreeMap(), 100000);
    testIterations(new TreeMap(), 1000000);
    testIterations(new TreeMap(), 10000000);
  }
}

Output

class java.util.Hashtable (1000), iterations: 3466937
class java.util.Hashtable (10000), iterations: 3553706
class java.util.Hashtable (100000), iterations: 3507707
class java.util.Hashtable (1000000), iterations: 3039724
class java.util.Hashtable (10000000), iterations: 871425
class java.util.HashMap (1000), iterations: 8712753
class java.util.HashMap (10000), iterations: 9193488
class java.util.HashMap (100000), iterations: 7523539
class java.util.HashMap (1000000), iterations: 5699614
class java.util.HashMap (10000000), iterations: 939541
class java.util.TreeMap (1000), iterations: 217810
class java.util.TreeMap (10000), iterations: 781167
class java.util.TreeMap (100000), iterations: 863536
class java.util.TreeMap (1000000), iterations: 290820
class java.util.TreeMap (10000000), iterations: 1044074

Other Related Posts

Map Examples

List Examples

Set Examples

Please Share Us on Social Media

Facebooktwitterredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *