1: Why is the hashmap capacity a power of 2? What is a load factor?
Capacity: the number of barrels.
Load factor: Number of elements/capacity.
is the power of 2 because the elements are more evenly distributed after they have been added to the bucket.
2: Why is string, integer such as wrapper class suitable as key?
Because it is immutable, final, and re-writes the hash () and Equals (). Immutable is necessary: To calculate hashcode, prevent the value of key from changing. The class you define can be a key to the map if it satisfies this requirement.
The 3:HASHMAP default capacity is 16, and the default reload factor is 0.75.
4:hashset VS TreeSet?
|--sortedset Interface--treeset Implementation class
Set interface-|--hashset Implementation class
|--linkedhashset Implementation Class
HashSet: does not guarantee that the order/not synchronous/maximum can put a null
When an element is deposited into the hashset, HashSet calls Hashcode () to determine where to store it.
The method of comparing the equality of two objects in HashSet is to compare the hashcode () values of the two objects for equality, if the two objects are not equal if they are not equal, to continue calling equals if the hashcode of the two objects are equal () The method further determines whether two objects are equal if the Equals () method returns true to think that two objects are equal and returns false to consider the two objects unequal.
TreeSet is an ordered set of elements in ascending order, the default is to sort by natural order, meaning that elements in the TreeSet to implement the comparable interface, we can construct the TreeSet object, passing the comparator object that implements the comparator interface.
HashSet is based on the hash algorithm , and its performance is usually better than treeset, and we should usually use HashSet.
Use HashMap when we need to determine whether it is equal; Sort of function when the door was used treeset;
Java collection classes