How to understand and master ConcurrentHashMap
This article mainly explains "how to understand and master ConcurrentHashMap". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand and master ConcurrentHashMap.
HashMap best practices
Now we know that in the actual project, we use HashMap as a container.
The capacity of the container, how many elements can be stored, and how many are appropriate in the first place? (initial capacity issue)
After specifying the initial capacity of the container, what if there are too many elements for the container to fit? (container expansion, loading factor problem)
In view of the above problems, let's analyze:
In HashMap, the default initial capacity size is 16. In the actual project, we can consider estimating the number of elements to be stored and setting the appropriate initial capacity according to the number of elements. Reduce the dynamic expansion of HashMap and rebuild hash tables, thus improving performance
/ * The default initial capacity-MUST be a power of two.* default initial capacity. The capacity of HashMap should be kept to the power of 2 to the power of n * / static final int DEFAULT_INITIAL_CAPACITY = 1