The first day of basic interview questions

1. ArrayList
expansion mechanism:
1. Use an array with a length of 0
2. Can use an array with a specified capacity
3. The first expansion is 10 , and the second expansion is 1.5 times the previous capacity (key point)
2. Iterator
1.Iterator is a kind of Interfaces for traversing elements in collection classes (such as List, Set, etc.)
2. Using an iterator to traverse a collection can only call the remove method to delete, and cannot modify the collection object, otherwise an exception will be thrown. 3. The
difference between linkList and ArrayList
1. Internal implementation : The bottom layer of linkList is a linked list, without index, slow query, fast addition and deletion. The bottom layer of ArrayList is an array, with index, fast query, slow addition and deletion.

2. From the perspective of memory: linList has no continuous memory, and the memory usage of newly added elements is small; ArrayList needs to reallocate memory space for the entire array when adding new elements, which takes up relatively large memory.

3. Iterator efficiency: Due to different internal implementations, LinkedList's iterator is more efficient because it does not need to additionally record the index position like ArrayList.

4. Thread safety: LinkedList is not thread-safe, and ArrayList is not thread-safe by default, but a thread-safe ArrayList can be obtained through the Collections.synchronizedList method.

5. Application scenarios: LinkedList is mainly used in scenarios with frequent insertion and deletion operations, while ArrayList is mainly used in scenarios with frequent access operations.
4.
Bottom layer of HashMap: based on hash algorithm, hash table
1. Data structure: jdk1.7: array + linked list, head insertion method; jdk1.8: array + linked list + red-black tree, tail insertion method
2. Red-black tree Balance adjustment strategy:
Hash bucket tree and anti-
tree If the number of elements in the tree is less than 6, the red-black tree is converted into a linked list to avoid waste of resources when the number of elements is small.
5. Index calculation: Calculate the position corresponding to each element in the hashmap through the hashCode value
6.put and expansion:
When creating an empty HashMap object, the bottom layer will not immediately create an array, only when the put() method is called for the first time ,
the bottom layer will create a Node array with a length of 16 , and insert elements into the array. When the number of elements
stored in the array exceeds the product of the load factor and the current capacity (the default is 0.75*16=12 ), the expansion operation will be triggered to double the capacity of the array and recalculate all elements in the new The index position in the hash table.

It should be noted that the hash value and index position of the element need to be recalculated during expansion, which may have a certain impact on the performance of the program. When we put an element into the HashMap, use the hashCode of the key to re-hash to calculate the subscript storage of the element of the current object in the array 
, if there is a key with the same hash value, there are two situations at this time.
If the key is the same, the original value will be overwritten;
if the key is different (a conflict occurs), the current key-value will be put into the linked list . 7.
Concurrency issues : jdk1. The tail insertion method is adopted to solve the problem of dead chain expansion. 8. Key design: 1. The key of HashMap can be null, and the implementation class of other maps cannot 2. The key of HashMap needs to implement the hashCode() and equals() methods to facilitate calculation Get the hash value of the key and look up the corresponding element in the hash table



 

Guess you like

Origin blog.csdn.net/weixin_71921932/article/details/129896882