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
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
- Hashtable Example
Simple example shows you step by step how to use Hashtable - HashMap Example
Simple example shows you step by step how to use HashMap - TreeMap Example
Simple example shows you step by step how to use TreeMap to sort a collection - EnumMap Example
Simple example shows you step by step how to use EnumMap for type-safety and speed of finite list of elements - WeakHashMap Example
Simple example shows you step by step how to use WeakHashMap - LinkedHashMap Example
Simple example shows you step by step how to use LinkedHashMap - Performance Comparison HashMap vs Hashtable vs TreeMap
Performance Comparison - Performance Comparison HashMap vs Hashtable vs TreeMap Benchmark Test
List Examples
- Stack Example
Simple example shows you step by step how to use Stack - Vector Example
Simple example shows you step by step how to use Vector - LinkedList Example
Simple example shows you step by step how to use LinkedList - ArrayList Example
Simple example shows you step by step how to use ArrayList - Performance Comparison between the four list implementations
Performance Comparison of ArrayList, LinkedList, Vector, and Stack - Performance Comparison ArrayList vs LinkedList
Performance Comparison - ArrayList vs LinkedList
Set Examples
- BitSet Example
Simple example shows you step by step how to use BitSet - EnumSet Example
Simple example shows you step by step how to use EnumSet - HashSet Example
Simple example shows you step by step how to use HashSet - TreeSet Example
Simple example shows you step by step how to use TreeSet - LinkedHashSet Example
Simple example shows you step by step how to use LinkedHashSet
Leave a Reply