Summary of frequently asked knowledge points about List, Set and Map interviews in Java

One: collection structure

1:List:

        Ordered (the order in which elements are stored in the collection and the order in which they are taken out are known), can be stored repeatedly , and has index subscripts . Commonly used implementation classes: ArrayList, LinkedList, Vector. The efficiency of finding elements is high , and the efficiency of inserting and deleting elements is low .

2:Set:

       Unordered (the order of storage and withdrawal may be inconsistent), non-repeatable storage , and no index subscript . Commonly used implementation classes: HashSet, LinkedHashSet, TreeSet. The efficiency of finding elements is low , and the efficiency of insertion and deletion is high.

3:Map:

        It is a collection of key-value pairs, (Key) key is unordered and unique ; (Value) value is allowed to be repeated . Commonly used implementation classes: HashMap, TreeMap, HashTable, LinkedHashMap, ConcurrentHashMap.

Two: The data structure used by the bottom layer of the collection

Collection:

        (1):List

                       ArrayList: Object array.

                       Vector: Object array.

                        LinkedList: A two-way circular linked list.

        (2):Set

                        HashSet ( unordered , unique ): implemented by HashMap.

                        TreeSet ( ordered , unique ): Red-black tree (self-balancing sorted binary tree).

        (3):Map

                        HashMap: Before JDK1.8, HashMap consisted of array + linked list . The array is the main body of HashMap, and the linked list exists to resolve hash value conflicts ("zipper method" to resolve conflicts). After JDK1.8, changes have been made in resolving hash value conflicts. When the length of the linked list is greater than the threshold (8 by default), the linked list is converted into a red-black tree to reduce conflicts .

                        LinkedHashMap: LinkedHashMap inherits from HashMap, and the bottom layer is still composed of array + linked list or red-black tree.

                        HashTable: composed of array + linked list. The array is the main body of the HashTable, and the linked list exists to resolve hash conflicts.

                        TreeMap: Red-black tree (self-balancing sorted binary tree).

Three: The realization principle of HashMap

        HashMap is implemented based on the Hash algorithm, and its specific judgment process is shown in the figure below.

Guess you like

Origin blog.csdn.net/qq_42027706/article/details/124574079