@org.junit.jupiter.api.Test
public void compareArrayListAndHashMapAndSet1(){
Map<Long, Long> map = new HashMap<>();
for (int i = 0; i < 1000_0000; i++) {
map.put(i+1L, i+1L);
}
long start = System.currentTimeMillis();
for (int i = 0; i < 1000_0000; i++) {
boolean b = map.containsKey(i + 1L);
}
long end = System.currentTimeMillis();
System.out.println("1000w元素 map耗时:" + (end-start));
List<Long> list = new ArrayList<>();
for (int i = 0; i < 10_0000; i++) {
list.add(i+1L);
}
long start2 = System.currentTimeMillis();
for (int i = 0; i < 10_0000; i++) {
boolean b = list.contains(i + 1L);
}
long end2 = System.currentTimeMillis();
System.out.println("10W元素 list耗时:" + (end2-start2));
Set<Long> set = new HashSet<>();
for (int i = 0; i < 1000_0000; i++) {
set.add(i+1L);
}
long start3 = System.currentTimeMillis();
for (int i = 0; i < 1000_0000; i++) {
boolean b = set.contains(i + 1L);
}
long end3 = System.currentTimeMillis();
System.out.println("1000w元素 set耗时:" + (end3-start3));
}
En conclusión:
La diferencia entre map y set no es grande
debido a la tabla hash utilizada en la capa inferior; debido a que solo almaceno un ID de tipo Long,
la velocidad de la lista cuando elijo elementos Set 1000w es demasiado lenta, ¡adiós!
Recomendar clave, valor para elegir HashMap;
Encontré una foto en línea:
Conéctese aquí: (Respete el original)
https://blog.csdn.net/high2011/article/details/106742629