HashMap and hashtable of the set

Prodigal son Tang Shao 2022-02-13 07:29:13 阅读数:458

hashmap hashtable set

HashMap、Hashtable The difference between


  • The underlying array + Linked list implementation , No matter what key still value Neither can it be. null, Thread safety , The way to achieve thread safety is to lock the whole when modifying data HashTable, Low efficiency .
  • initial size by 11, Capacity expansion :newsize = olesize*2+1


hashMap Of jdk7 and jdk8 There's a difference ;

1. Realization way

jdk7 Using arrays + Linked list ,jdk8 The array used + Linked list + Red and black trees

2. When a new node is inserted into the linked list, the insertion order is different

jdk7 Insert in the head ( It is easy to cause the problem of dead loop when multithreading capacity expansion ),jdk8 Insert at the end

Common characteristics :

  • Can be stored null Key sum null value , Thread unsafe
  • initial size by 16, Capacity expansion :newsize = oldsize*2,size It must be 2 Of n The next power
  • The expansion is for the whole Map, At each expansion , The elements in the original array are recalculated in turn , And reinsert
copyright:author[Prodigal son Tang Shao],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130729117178.html