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.
01 | package bob.blog; |
02 |
03 | import java.util.Collections; |
04 | import java.util.HashMap; |
05 | import java.util.Hashtable; |
06 | import java.util.Map; |
07 | import java.util.Map.Entry; |
08 |
09 | /** |
10 | * The Class HashtableVsHashmap. |
11 | */ |
12 | public 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