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.
package bob.blog; import java.util.Collections; import java.util.HashMap; import java.util.Hashtable; import java.util.Map; import java.util.Map.Entry; /** * The Class HashtableVsHashmap. */ public class HashtableVsHashmap { private static Map< String, String > map; private static String bob = new String("bob"); private static final int iter = 500000; private static long start = 0; private static long end = 0; /** * The main method. * * @param args the arguments */ public static void main(final String[] args) { hashtablePerf(); hashmapPerf(false); hashmapPerf(true); } /** * Hashmap perf. */ private static void hashmapPerf(final boolean syncd) { // HashMap map = new HashMap< String, String >(); if (syncd) { map = Collections.synchronizedMap(map); } start = System.currentTimeMillis(); for (int i = 0; i < iter; i++) { map.put(bob + i, bob); } map.put(null, null); end = System.currentTimeMillis(); System.out.println("Inserting HashMap: " + (end - start) + " ms - Synchronized: " + syncd); start = System.currentTimeMillis(); for (int i = 0; i < iter; i++) { map.get(bob + i); } end = System.currentTimeMillis(); System.out.println("Reading HashMap: " + (end - start) + " ms - Synchronized: " + syncd); start = System.currentTimeMillis(); for(Entry< String, String > e : map.entrySet()){ String val = e.getValue(); } end = System.currentTimeMillis(); System.out.println("Reading HashMap by Entry: " + (end - start) + " - Synchronized: " + syncd); } /** * Hashtable perf. */ private static void hashtablePerf() { // Hashtable map = new Hashtable< String, String >(); start = System.currentTimeMillis(); for (int i = 0; i < iter; i++) { map.put(bob + i, bob); } end = System.currentTimeMillis(); System.out.println("Inserting Hashtable: " + (end - start) + " ms"); start = System.currentTimeMillis(); for (int i = 0; i < iter; i++) { map.get(bob + i); } end = System.currentTimeMillis(); System.out.println("Reading Hashtable: " + (end - start) + " ms"); start = System.currentTimeMillis(); for(Entry< String, String > e : map.entrySet()){ String val = e.getValue(); } end = System.currentTimeMillis(); System.out.println("Reading Hashtable by Entry: " + (end - start) + " ms"); } }Output:
Inserting Hashtable: 640 ms
Reading Hashtable: 172 ms
Reading Hashtable by Entry: 31 ms
Inserting HashMap: 610 ms - Synchronized: false
Reading HashMap: 265 ms - Synchronized: false
Reading HashMap by Entry: 47 - Synchronized: false
Inserting HashMap: 625 ms - Synchronized: true
Reading HashMap: 282 ms - Synchronized: true
Reading HashMap by Entry: 46 - Synchronized: true
few differences:
ReplyDelete1) hashtable is synchronized while hashmap is not.
2) hashmap is fast while hashtable is slow
3) hashtable is old but hashmap is new
4) hashtable supports enumeration while hashmap uses Iterator
souce: difference between hashtable and hashmap in Java
Đặt vé máy bay tại Aivivu, tham khảo
ReplyDeletesăn vé máy bay giá rẻ đi Mỹ
ve may bay tu my ve vietnam
các chuyến bay từ đức về việt nam hôm nay
vé máy bay từ nga về việt nam bao nhiêu
vé máy bay từ anh về việt nam
chuyến bay từ pháp về việt nam hôm nay
chi phi ve may bay cho chuyen gia nuoc ngoai