HashMap和TreeMap

hashMap

HashMap工做原理 我对hashMap的认识基本是对的,hashCode() and equals() is important for hashMap. 除了这个彻底不知道html

当从新调整HashMap大小的时候,确实存在条件竞争,由于若是两个线程都发现HashMap须要从新调整大小了,它们会同时试着调整大小。在调整大小的过程当中,存储在链表中的元素的次序会反过来,由于移动到新的bucket位置的时候,HashMap并不会将元素放在链表的尾部,而是放在头部,这是为了不尾部遍历(tail traversing)。若是条件竞争发生了,那么就死循环了。这个时候,你能够质问面试官,为何这么奇怪,要在多线程的环境下使用HashMap呢?:)java

TreeMap

TreeMap is base on RB-Tree.面试

  • 若是键值没有实现java.lang.Comparable是真的不行的。。。

A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations. Algorithms are adaptations of those in Cormen, Leiserson, and Rivest's Introduction to Algorithms.多线程

相关文章
相关标签/搜索