preface

Welcome to our GitHub repository Star: github.com/bin39232820… The best time to plant a tree was ten years ago, followed by now

omg

Today we are done with Set,List,Map, and Set. We sometimes use Set, but we also have a method for Stream to be de-weighted, so we don’t use it very much now. I’m going to use HashSet, and I’m going to have a chance to write a functional programming Stream.

🔥 Introduction to the most complete Collection of Java containers 🔥 the most complete collection of Java containers the most basic data structures (hand tear list) 🔥 the most complete collection of Java containers the most complete collection of Java containers the most complete collection of Java containers the most complete collection of Java containers the most complete collection of Java containers the most complete collection of Java containers the most complete collection of Java containers the most complete collection of Java containers the most complete collection of Java containers the most complete collection of Java containers 🔥 Equals and hashCode 🔥 🔥 ConcurrentHashMap 🔥 Equals and hashCode 🔥 🔥 LinkedHashMap, the most complete collection of Java containers ever

HashSet overview

At more than 300 lines of code, HashSet is the smallest collection OF code I’ve ever seen. Quite simply, it has the following characteristics

  • A collection with no repeating elements. The underlying implementation is based on HashMap.
  • Not thread-safe, create a thread safe HashMap can use Collections. SynchronizedSet.

HashSet source code interpretation

Inheritance relationships

  • AbstractSet abstract class to implement the Set interface
  • Implement java.io.Serialization interface, support Serialization
  • Cloneable interface, support object cloning, shallow replication

attribute

Private TRANSIENT HashMap<E,Object> map; private transient HashMap<E,Object> map; Private static final Object PRESENT = New Object();Copy the code

A constructor

Public HashSet() {map = new HashMap<>(); public HashSet() {map = new HashMap<>(); Public HashSet(Collection<?) public HashSet(Collection<? extends E> c) { map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16)); addAll(c); } // Initialize the map with the specified capacity size and load factor, Public HashSet(int initialCapacity, float loadFactor) {map = new HashMap<>(initialCapacity, loadFactor); Public HashSet(int initialCapacity) {map = new HashMap<>(initialCapacity); } // Package access, Construct empty LinkedHashSet HashSet(int initialCapacity, float loadFactor, boolean dummy) { map = new LinkedHashMap<>(initialCapacity, loadFactor);Copy the code

methods

  • Size () method: Internally calls Map’s size method.
    public int size() {
        return map.size();
    }
Copy the code
  • The isEmpty() method, which also calls map, checks whether size is null by checking whether size is 0
 public boolean isEmpty() {
        return map.isEmpty();
    }
Copy the code
  • The add () method

Inside the add method of a HashSet is the hashmap.put () method for adding keys. Inside a HashMap is putVal()

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

conclusion

Use HashMap keys to store data in a HashMap. Use HashMap keys to store data in a HashMap

  • The implementation of a HashSet supports null keys, and the interior of a HashSet does not support duplicate keys
  • Don’t repeat
  • Thread insecurity

eggs

TreeSet is a red black tree, and TreeMap is a red black tree. This means the end of the Java container, and at least we’ve gone over most of the commonly used containers, even in an interview.

We know that ArrayList LinkedList HashMap HashSet are thread-safe, but one class that can make them thread-safe is Collections

  • Collections.synchronizedList
  • Collections.synchronizedMap
  • Collections.synchronizedSet

The Easter egg is let’s decrypt the method, pick one of them

The Collections. SynchronizedList source List (List) method

Under normal circumstances, the Collections. SynchronizedList List (List) returns a synchronizedList object, the object will be on the List in the form of combination method of interface operation, entrusted to the incoming List object, And lock all interface method objects to obtain concurrency security.

public static <T> List<T> synchronizedList(List<T> list) {
        return (list instanceof RandomAccess ?
                new SynchronizedRandomAccessList<>(list) :
                new SynchronizedList<>(list));
}

Copy the code

We can see that it’s really just a new SynchronizedList, so where is this class? Keep going

static class SynchronizedList<E> extends SynchronizedCollection<E> implements List<E> { ... . final List<E> list; SynchronizedList(List<E> list) { super(list); this.list = list; } SynchronizedList(List<E> list, Object mutex) { super(list, mutex); this.list = list; }... . public E get(int index) { synchronized (mutex) {return list.get(index); } } public E set(int index, E element) { synchronized (mutex) {return list.set(index, element); } } public void add(int index, E element) { synchronized (mutex) {list.add(index, element); } } public E remove(int index) { synchronized (mutex) {return list.remove(index); }}... . }Copy the code

As you can see, SynchronizedList implements mutex locks for get, set, add, and so on, and delegates the operations to the initial list. This is a combination of non-thread-safe objects encapsulated as thread-safe objects, and the actual operation is on the original non-thread-safe objects, but the synchronization lock is placed before the operation.

Because there are many business scenarios with this requirement, the Utility class is packaged in the Java class library for use by the required modules.

However, it is worth noting that ConcurrentHashMap is more efficient when it comes to secure sets of maps. After all, the granularity of object locks is coarser.

Release notes

  • The source code here is JDK8 version, different versions may vary, but the basic principle is the same.

At the end

I have a goal to write two or three articles a week. I hope I can keep it up for a year. I hope you can give me more suggestions so that I can learn more and make progress together.

Daily for praise

Ok, everybody, that’s all the content of this article, can see here people ah, are god.

Creation is not easy, your support and recognition, is the biggest motivation for my creation, we will see in the next article

Six pulse excalibur | article “original” if there are any errors in this blog, please give criticisms, be obliged!