The HashMap list exceeds 8 reasons converted into red-black trees

HashMap JDK1.8 introduced and later versions of the red-black tree structure, if the number of elements in the list greater than or equal tub 8, is converted into a tree structure list; 6 if the number of list elements in the tub or less, reducing the tree structure into a linked list. Since the average length of the red-black tree lookup is log (n), when a length of 8, the average length of 3, if the list continue to be used, the average length of 8/2 = 4, this has the necessary conversion of the tree. If the chain length is less than or equal 6,6 / 2 = 3, although the speed is fast, but the time into the spanning tree and not too short.

6 and 8 have a choice, there is a difference between the intermediate 7 can effectively prevent frequent switching trees and linked lists. Hypothetically, if the number of the list is designed to be converted into more than eight chain tree structure, linked list is less than the number of converter 8 into a tree structure list, if a HashMap kept inserted, deleted elements, the number of list hovering around 8 to frequently occurring tree turn linked list tree turn, would be very inefficient.

Reprinted to https://blog.csdn.net/xingfei_work/article/details/79637878

Guess you like

Origin blog.csdn.net/LWHuai/article/details/94579973