This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

Set is one of the most commonly used collections in Java, and its key implementation classes include HashSet, LinkedHashSet, and TreeSet.

HashSet

A HashSet stores elements in a hash table, which is underlying a HashMap. The data of a HashSet is the key of a HashMap, and the value of a HashMap is an empty object.

  1. What’s underneath a HashSet?

As you can see from its constructor, when we create a HashSet Object, we create a HashMap with the specified key and the value of the empty Object type.

 // The variable used to store HashSet data
 private transient HashMap<E,Object> map;
 
 public HashSet(a) {
     map = new HashMap<>();
 }
 
 public boolean add(E e) {
     // PRESENT is an empty Object of type Object
     return map.put(e, PRESENT)==null;
 }
Copy the code
  1. What is the initial capacity and load factor of a HashSet?

Because the underlying HashSet is a HashMap, it has an initial capacity of 16 and a load factor of 0.75. This value can also be inferred from its constructor.

 public HashSet(Collection<? extends E> c) {
     map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1.16)); . }Copy the code
  1. Does a HashSet guarantee the order of elements?

A HashSet stores elements through a hash table, so elements inserted into a HashSet are generally considered unordered. However, you can also preserve the order in which data is inserted by specifying the constructor (which has permission of Default), with the underlying LinkedHashMap. This is why linkedhashsets maintain elements in order.

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

LinkedHashSet

The underlying LinkedHashSet is LinkedHashMap, whose linked list ensures the insertion and traversal order of elements, and hash table ensures the uniqueness of elements. LinkedHashSet also has an initial capacity of 16 and a load factor of 0.75.

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

TreeSet

TreeSet stores data through a binary tree, with TreeMap at the bottom. The binary tree structure guarantees the order of elements. The default is natural sorting, which can be customized by implementing the Compareable interface and rewriting the compareTo() method inside.

 public TreeSet(a) {
     this(new TreeMap<E,Object>());
 }
Copy the code

Difference between HashSet and LinkedHashSet

A HashSet does not guarantee the order of elements, whereas a LinkedHashSet does.

Difference between TreeSet and LinkedHashSet

LinkedHashSet ensures that the insertion and traversal order of elements are consistent, while TreeSet is ordered according to rules during insertion, and its orderliness has different meanings.

conclusion

HashSet, LinkedHashSet, and TreeSet are all thread unsafe, and in a multithreaded environment, You can use the Collections. SynchronizedSet (new HashSet < > ()) or new CopyOnWriteArraySet < > ().