Monday, July 5, 2010

Hashtable vs. HashMap and a little thing called Entry

This was just to check if there are any real performance issues between Hashtable, HashMap and Collections.synchronizedMap(map), and as it turns out... it ain't that that bad... and even though the Hashtable is synchronized it still reads faster.

Something else to note, iterating via the entry set far out performs a regular get.
So when sequentially going through a map it's probably the way to go.

Hashtable

Does NOT allow null in either the key or the value. Will throw java.lang.NullPointerException.
It is synchronized.

HashMap

Allows null in either the key or the value.
It is NOT synchronized.

01package bob.blog;
02
03import java.util.Collections;
04import java.util.HashMap;
05import java.util.Hashtable;
06import java.util.Map;
07import java.util.Map.Entry;
08
09/**
10 * The Class HashtableVsHashmap.
11 */
12public class HashtableVsHashmap {
13
14 private static Map<> map;
15
16 private static String bob = new String("bob");
17
18 private static final int iter = 500000;
19
20 private static long start = 0;
21
22 private static long end = 0;
23
24 /**
25 * The main method.
26 *
27 * @param args the arguments
28 */
29 public static void main(final String[] args) {
30
31 hashtablePerf();
32 hashmapPerf(false);
33 hashmapPerf(true);
34 }
35
36 /**
37 * Hashmap perf.
38 */

No comments:

Post a Comment