HashSet from entry to soil

HashSet from entry to soil

What is Hashset

​ Below is the inheritance diagram of HashSet

[External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-bMUfHjgu-1593755190238)(https://raw.githubusercontent.com/iszhonghu/Picture-bed/master/img /20200702171047.png)]

HashSet internally uses HashMap

​ When we look at the construction method of HashSet, we will find that a HashMap is directly new and assigned to the map attribute.

​ That is, HashSet is a shell on the HashMap, the method is relatively simple, each method is actually a corresponding operation map.

How to remove heavy HashSet

​ Because HashSet inherits Set, there can be no duplicates. In most cases, we use HashSet because of its de-duplication function.

​ To achieve deduplication, HashSet starts from the add method

public boolean add(E e) {
    
    
        return map.put(e, PRESENT)==null;
    }

​ From here, we can see that the add method of HashSet calls the put method of HashMap, but what puts in is a key-value pair, and HashSet is not a key-value pair, but a generic.

​ So HashSet regards the value you want to store as the key, and the corresponding value is a final Object object, which only serves as a placeholder. At this time, because HashMap does not allow key duplication, it happens to be used by HashSet to ensure that it is not repeated.

Non-thread safe

​ Since HashMap is not thread-safe, HashSet is not thread-safe, so use it with caution in multi-threaded and high-concurrency situations.

​ And HashSet does not have a multi-threaded version like Hashmap. If you want to use multi-threaded mode, use the following:

Set<String> set = Collections.synchronizedSet(new HashSet<String>());

​ Or use a static inner class of ConcurrentHashMap that implements the Set interface.

LinkedHashSet

​ Similar LinkedHashSet is also a shelled LinkedHashMap. Compared with HashSet, its characteristic is to ensure that the data is in order, in what order when inserting, and in what order when traversing

No-argument constructor

 public LinkedHashSet() {
    
    
        super(16, .75f, true);
    }

Since LinkHashSet inherits from HashSet, it actually calls the constructor of the three parameters of HashSet

HashSet(int initialCapacity, float loadFactor, boolean dummy) {
    
    
  map = new LinkedHashMap<>(initialCapacity, loadFactor);
}


This is not new but a new HashMap of a LinkedHashMap, this is the key to ensuring that it orderly, LinkedHashMap OA-game losing streak with a two-way manner on the basis of additional HashMap holds the key to the insertion order

​ Because LinkedHashMap can guarantee the order of key-value pairs, it is used to implement simple LRU caching.

​ So when you want to ensure that the elements are not repeated, but also to ensure that the elements are in order, you can use LinkedHashSet

At last

  • If you feel that you are rewarded after reading, I hope to give me a thumbs up. This will be my biggest motivation for updating. Thank you for your support.
  • Welcome everyone to pay attention to my official account [java Toka Fox], focus on the basic knowledge of java and computer, and ensure that you will get something after reading it. If you don’t believe me, hit me
  • If you have different opinions or suggestions after reading, welcome to comment and share. Thank you for your support and love.

Guess you like

Origin blog.csdn.net/issunmingzhi/article/details/107104219