Android interview questions detailed algorithm and data structure interview questions summary

text

Please talk about the principle of HashMap and SparseArrary, the advantages of SparseArrary compared to HashMap, how does ConcurrentHashMap achieve thread safety?

What is this question trying to investigate?

1. HashMap, SparseArrary basic principle?

2. What are the advantages of SparseArrary over HashMap?

3. How does ConcurrentHashMap achieve thread safety?

Knowledge points of inspection

HashMap,SparseArrary、ConcurrentHashMap

How Candidates Answer

Both HashMap and SparseArray are used to store Key-value type data.

The difference between SparseArray and HashMap:

Double array, delete O(1), binary search

  • In terms of data structure: hashmap uses a linked list. sparsearray uses a double array.
  • In terms of performance: hashmap has a default length of 16 and will be automatically boxed. If the key is an int, the hashmap must first be encapsulated into an Integer. sparseArray will be directly converted to int. So the limitation used by spaseArray is that the key is int. The amount of data is less than 1k. If the key is not int and less than 1000. Arraymap can be used.
The basic principle of HashMap

HashMap internally uses an array with a default capacity of 16 to store data, and each element in the array is the head node of a linked list, so, more precisely, the internal storage structure of HashMap uses a hash table Zipper structure (array + linked list).

insert image description here

The default storage size in HashMap is an array with a capacity of 16, so when we create a HashMap object, even if there are no elements in it, we need to allocate a memory space for it, and we continue to put data into the HashMap , when a certain capacity limit is reached, HashMap will automatically expand.

The basic principle of SparseArray

SparseArray is more memory-efficient than HashMap, and has better performance under certain conditions, mainly because it avoids the automatic boxing of keys (int is converted to Integer type), and it stores data internally through two arrays. One stores the key, and the other stores the value. In order to optimize performance, it internally compresses the data to represent the data of the sparse array, thereby saving memory space. We can see from the source code that the key and value are represented by an array :

private int[] mKeys;
private Object[] mValues;

We can see that SparseArray can only store data whose key is int type. At the same time, SparseArray uses the binary search method when storing and reading data.

public void put(int key, E value) {
    
    
        int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
        ...
        }
 public E get(int key, E valueIfKeyNotFound) {
    
    
        int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
        ...
        }

That is, when adding data in put, the binary search method will be used to compare the size of the key of the element we are currently adding with the previous key, and then arranged in order from small to large, so the elements stored in SparseArray are all element by element The key values ​​are arranged from small to large. When acquiring data, the binary search method is also used to determine the position of the element, so it is very fast when acquiring data, much faster than HashMap, because HashMap acquires data by traversing the Entry[] array to obtain the corresponding element.

adding data

public void put(int key, E value)

delete data

public void remove(int key)

retrieve data

public E get(int key)
  
public E get(int key, E valueIfKeyNotFound)

Although the performance of SparseArray is relatively good, because adding, searching, and deleting data requires a binary search first, the performance is not obvious when the amount of data is large, and it will be reduced by at least 50%. We can use SparseArray instead of HashMap if the following two conditions are met:

  • The amount of data is not large, preferably within a thousand levels
  • The key must be of int type, and the HashMap in this case can be replaced by SparseArray:
The basic principle of ConcurrentHashMap
  • The implementation of JDK1.8 reduces the granularity of locks. The granularity of JDK1.7 locks is based on Segment and contains multiple HashEntry, while the granularity of JDK1.8 locks is HashEntry.

  • The data structure of the JDK1.8 version has become simpler, making the operation clearer and smoother. Because synchronized has been used for synchronization, the concept of segment lock is not needed, and the data structure of Segment is no longer needed. Due to the granularity Reduced, the implementation complexity also increased.

  • JDK1.8 uses red-black tree to optimize the linked list. The traversal based on the long-length linked list is a very long process, but the traversal efficiency of the red-black tree is very fast, replacing the linked list with a certain threshold.

insert image description here

End of article

More Android interview questions can be scanned for free!

↓↓↓【Preview】↓↓↓

img

Guess you like

Origin blog.csdn.net/Android_XG/article/details/130784955