Interview Summary (3)

data structure

        Data structure is the way computers store and organize data, and it is the basis and core of algorithms. It can help us process data more efficiently, improve program execution efficiency, and reduce memory usage, making programs more stable, reliable, and scalable. Data structure can be divided into linear structure, tree structure, graph structure, etc., each structure has its unique characteristics and applicable scenarios;

Analyze the LinkedList data structure in JAVA

        LinkedList is a doubly linked list data structure in Java. It implements the List interface, and also implements the Queue and Deque interfaces. It can be used as a queue, stack, and two-way queue. Compared with ArrayList, LinkedList is characterized by higher efficiency of inserting and deleting elements, but lower efficiency of query and traversal!

        In LinkedList, each element is a node, and the node contains the value of the current element and references to the predecessor and successor nodes. The head node of LinkedList does not contain any actual elements, but only serves as the starting point of the linked list;

The basic operations of LinkedList are as follows:

1. Add elements at the end of the linked list, time complexity O(1);

2. Add elements at the head of the linked list, time complexity O(1);

3. To add an element at a specified position in the linked list, you need to traverse the linked list to find the position, and the time complexity is O(n);

4. To delete the specified element in the linked list, you need to traverse the linked list to find the element, and the time complexity is O(n);

5. Delete the head node or tail node in the linked list, the time complexity is O(1);

6. To get the element at the specified position in the linked list, you need to traverse the linked list to find the position, and the time complexity is O(1);

Thread safety: In a multi-threaded environment, LinkedList is not thread-safe and requires synchronization control, and frequent insertion and deletion operations will affect performance;

Analyze the ArrayList data structure in Java

        ArrayList is a dynamic data storage structure in Java. It implements the List interface, can access elements through subscripts, supports dynamic expansion and contraction, and can store any type of object;

        In ArrayList, elements are stored in a continuous memory space, and each element can be accessed through a subscript, which starts from 0. ArrayList has an array with a default initial capacity of 10. When the number of elements reaches the capacity of the array, it will The automatic expansion is 1.5 times of the original capacity. When the number of elements is less than half of the array capacity, it will automatically shrink to half of the original;

ArrayList supports the following basic operations:

1. Add elements at the end of the array, the time complexity is O(1);

2. To add an element at a specified position in the array, the elements after the insertion position need to be shifted back one bit in turn, and the time complexity is O(n);

3. To delete the specified element in the array, the elements after the insertion position need to be moved forward one by one, and the time complexity is O(n);

4. Delete the head element or tail element of the array, the time complexity is O(1);

5. Get the element at the specified position in the array, the time complexity is O(1);

6. Traversing the array, taking out the array elements one by one for operation, the time complexity is O(n);

Thread safety: In a multi-threaded environment, ArrayList is not thread-safe and needs to be controlled synchronously. At the same time, when using ArrayList, it is necessary to pay attention to the performance overhead caused by the expansion and shrinkage of the array, and minimize the frequency of the array. Scaling and shrinking operations;

Analyze the HashMap data structure in Java

        HashMap is a commonly used data structure in Java. It mainly implements the Map interface, which can map keys (Key) to values ​​(Value). The following is a simple analysis of HashMap:

        Principle : The bottom layer of HashMap is implemented by array + linked list/red-black tree. Arrays are used to store elements, and the storage location of elements is obtained through the hashCode() method of key. There may be hash conflicts. When hash conflicts occur, linked lists will be used Or data structures such as red-black trees to store values;

        Hash conflicts : Hash conflicts are inevitable in hash algorithms, so HashMap needs to handle hash conflicts when inserting elements, and values ​​can be stored through data structures such as linked lists and red-black trees. In JDK8, when the length of the linked list reaches 8, the linked list is converted into a red-black tree, and when the number of red-black tree nodes is less than 6, the red-black tree degenerates into a linked list;

        Expansion mechanism : When the number of elements in the HashMap exceeds the load factor (the default is 0.75) , the expansion operation will be performed automatically, the capacity will be doubled, and all elements will be re-hashed and stored in a new array;

        Concurrency : HashMap has thread safety issues in a multi-threaded environment, because multiple threads may operate on the same hash bucket at the same time, which may cause problems such as linked list formation. In JDK8, HashMap uses mechanisms such as CAS and synchronized to ensure thread safety;

        Performance optimization : In order to improve the performance of HashMap, you can optimize it by specifying the initial capacity, load factor, and using LinkedHashMap. At the same time, in order to avoid hash conflicts, you can optimize the hash function and use a better hash algorithm for optimization;

Guess you like

Origin blog.csdn.net/weixin_64625868/article/details/129352809