me encontré con un algoritmo en la red http://www.coderanch.com/t/201836/Performance/java/Hashtable-vs-Hashmap y decidimos probarloson HashMaps con capacidad predefinida más rápido
public class MapTest{
static int sizeOfTrial = 100000;
static String[] keys = new String[sizeOfTrial];
static String[] vals = new String[sizeOfTrial];
public static void main(String[] args) {
//init sizeOfTrial key/value pairs
for (int i=0; i < sizeOfTrial; i++){
String s1 = "key"+ i;
String s2 = "val"+ i;
keys[i] = s1;
vals[i] = s2;
}
test(new TreeMap(), "TreeMap");
test(new Hashtable(), "Hashtable");
test(new HashMap(), "HashMap");
test(new Hashtable(200000), "Hashtable presized");
test(new HashMap(200000), "HashMap presized");
}
public static void test(Map tm, String name){
long t1 = System.currentTimeMillis();
for (int i=0; i < sizeOfTrial; i++){
tm.put(keys[i],vals[i]);
}
for (int i=0; i < sizeOfTrial; i++){
tm.get(keys[i]);
}
long t2 = System.currentTimeMillis();
System.out.println("total time for " + name + ": " + (t2-t1));
}
}
y me dieron los siguientes resultados
total time for TreeMap: 1744
total time for Hashtable: 446
total time for HashMap: 234
total time for Hashtable presized: 209
total time for HashMap presized: 196
¿Es esta JVM dependiente y arbitraria ¿o realmente proporciona un acceso y un tiempo de almacenamiento más rápidos?
Esto solo es válido si no se excede el tamaño predefinido. Si es así, el predefinido también hará una copia. – twain249
@ twain249: cierto. Aclarado con las palabras "como a menudo". – mellamokb
¿Qué hay de la recuperación, sino también el almacenamiento de nuevos valores en contra de una vieja clave ... ¿Será más rápido bajo esa circunstancia? – Nav