HashSet storage mechanism:
- With a length of the underlying storage element array, and the array is always the n-th power of 2, if the incoming length of the array is not n-th power of 2, HashSet it will automatically extend to the n-th power of 2;
- HashSet constructor:
HashSet(int initialCapacity, float loadFactor) //initialCapacity——控制底层数组长度 //loadFactor——当HashSet感觉到底层快要满的时候(0.75),会将底层数组容量扩充1倍, //原来的数组就变成垃圾,等待回收,即rehash(重hash) //说明:当LoadFactor为0.75时,75%的桶已满,HashSet会分配新的数组;LoadFactor越小, //越消耗内存;LoadFactor越大,性能越低 //数组越满,才回去“重hash”,越有可能出现链表
- HashSet into the mechanism:
- When all elements add to the mix, the object is invoked HashSet HashCode () method, to obtain an int value;
- The int value returned HashCode (), which is calculated in the bottom HashSet [] array of storage locations (indexed array);
- If no element to be added is a position (is empty), directly into the can;
- If you want to add the element position has been, where it will form a "chain"
- HashSet take elements of mechanisms:
- When we want to take an element, the object is invoked HashSet HashCode () method, to obtain an int value;
- The int value returned HashCode (), which calculates the storage position (the index of the array) at a HashSet;
- If the location is exactly looking element can be taken directly;
- If the location has a list, HashSet to "one by one" in the search list elements
Description: In the best case, the performance is almost HashSet array match, to deposit, withdraw very high performance
- HashSet two objects are considered equal conditions:
- HashCode two objects () is equal to the return value;
- Compare the two objects are also returned by equals True
Detailed usage of Java HashSet function, storage mechanisms and performance analysis
Guess you like
Origin blog.csdn.net/qq_38358499/article/details/100631653
Recommended
Ranking