Common Java Collections Interview Questions

1. Talk about the put method in the Map collection:

        First encapsulate the key and value into a node object, then call the hashcode method of k at the bottom layer to get the hashcode value, use the hash algorithm to convert this value into the subscript of the array, if there is no element in the subscript position, then the node will be The object is added to this position. If there is an element at this position, then the key in the node object and the key in each node of the linked list at this position will be equals. If both return false, then the node object will be Add to the end of the linked list (it is added to the end after jdk1.8), if there is a method that returns true, then the value of the node will be overwritten

2. Talk about the get method in the Map collection:

        The bottom layer calls the hashcode method of the key to get the hashcode value, and uses the hash algorithm to convert this value into the subscript of the array. If there is no element at the subscript position, then the get method returns null. If there is an element at the position, then it will get The key and the key in each node of the linked list at this position are equals. If each method returns false, then the value returned by the get method is also null. If there is an equals method that returns true, then the get method returns is the value of the node

3. Why do the key objects placed in the Map collection rewrite the hashcode and equals methods

       If the hashcode method is not rewritten, the hash distribution will be uneven, and the reason for rewriting equals is because the key is an object, and we need to compare the contents of the object

4. Features of the key part of the HashMap collection:

        Unordered and non-repeatable.
        Why is it out of order: because the result obtained by the hash algorithm is random and
        non-repeatable, how is it guaranteed? The equals method is used to ensure that the keys of the HashMap collection cannot be repeated. If the key is duplicated, the value will be overwritten.
        HashSet is placed in the key part of the HashMap collection, so the elements in the Hashset also need to rewrite the hashcode and equals methods

5. What is uneven hash distribution:

        ‌When the return values ​​of the hashcode method are all the same value, then it will become a one-way linked list‌When the
        return values ​​​​of the hashcode method are all different, then it will become a pure array

6. HashMap features:

        First of all, its bottom layer is a hash table data structure, which is not thread-safe. The default initialization capacity is 16, and the default load factor is 0.75. This default load factor means that when the capacity of the underlying array of the HashMap collection reaches 75%, the array starts to expand. , Twice the expansion, when manually specifying the initial capacity, the value of this capacity must be a power of 2, which is necessary to achieve uniform hash distribution and improve the access efficiency of HashMap.

        If the value passed in when initializing the capacity is not a power of 2, then the method will be called to convert that number to the smallest power of 2 that is greater than that number. After the single-linked list exceeds 8 elements, the single-linked list will turn
        red Black tree structure, when the elements in the linked list are less than 8, the red and black numbers will change back to a single linked list

7. Hashtable collection:

        The initial capacity of Hashtable is 11, the load factor is 0.75, and the expansion: original capacity * 2+1, is thread-safe

8. The difference between Hashtable and HashMap collections:

        The key and value in the Hashtable collection cannot store null, and
        the key and value in the thread-safe HashMap collection can be null, which is not thread-safe

9. Other

Properties collection:
Properties is a Map collection that inherits the Hashtable collection. The key and value of Properties are both String types.
Properties are called attribute class objects.
Properties are thread-safe
Save: setproperties
Get: getproperties

 

The bottom layer of the TreeMap collection is a binary tree

10. arraylist set:

        The bottom layer is an object array, with a default initialization capacity of 10 (the bottom layer first creates an empty array, and when adding the first data, the initialization capacity is 10), and the arraylist is not linearly safe, and then the arraylist is a 1.5 times expanded arraylist
. That is, the query efficiency is high, and the efficiency of inserting or deleting data at the specified position is low, but if elements are added or deleted at the end of the arraylist collection, its efficiency is also quite high

11.voecat collection

        The bottom layer is the object array, with a default initial capacity of 10 and twice the capacity, because voecat is thread-safe, so the efficiency will be lower than that of the arraylist collection

12. linkedlist:

        The underlying doubly linked list, so the efficiency of inserting or deleting elements will be high, and the query efficiency will be low, and linkedlist is not linearly safe

13. The difference between arraylist and linkedlist:


        The bottom layer of arraylist is an object array, and the bottom layer of linkedlist         is a two-way linked
        list
        . So using arraylist is more than using linkedlist

14. The difference between arraylist and vector

        First of all, the bottom layer of these two collections is an object array, and the default initialization capacity is 10, but the arraylist is 1.5 times the expansion, and the vector is twice the expansion. Arraylist is not
        thread-safe, and vector is thread-safe, so in terms of efficiency, Arraylist is more efficient, and arraylist is given priority in development

15.set interface (unordered and non-repeatable)

        hashset: the key part of the underlying hashmap collection

        treeset: the bottom layer is a binary tree, treeset can be sorted

 

Guess you like

Origin blog.csdn.net/qq_26112725/article/details/131096775