Inheritance relationships

1. Implement Serializable interface, that is, support serialization.

2.TreeSet implements the Cloneable interface, meaning it can be cloned.

3. Implement the Iterable interface, that is, use foreach iterator to iterate through the collection elements.

4.TreeSet implements the NavigableSet interface, which means it supports a number of navigation methods. Such as finding the best match for a specified target.

AbstractSet > AbstractSet > AbstractSet > AbstractSet > AbstractSet > AbstractSet > AbstractSet > AbstractSet > AbstractSet > element1.

It is not repeated and contains at most one NULL.

Basic attributes

private transient NavigableMap<E,Object> m;
Copy the code
 	private static final Object PRESENT = new Object();

    
Copy the code

Constructor and initialization

1. Use the NavigableMap constructor

Constructs a set backed by the specified navigable map. Constructs a set backed by the specified navigable map. Constructs a set backed by the specified navigable map. */ TreeSet(NavigableMap<E,Object> m) { this.m = m; }Copy the code

2. Default constructor. Using this constructor, the elements in TreeSet are arranged in a natural order.

TreeMap for underlying use

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

3. Pass in the constructor for the comparator

// Create a new TreeMap in custom sort, // create a TreeSet based on the TreeSet, // use the TreeMap key to save the Set element. super E> comparator) { this(new TreeMap<>(comparator)); }Copy the code

4. Call the method 2 constructor to create a TreeSet, with the underlying TreeMap holding the collection elements

public TreeSet(Collection<? extends E> c) { this(); // addAll elements from Collection c to TreeSet; }Copy the code

5. Call the method 3 constructor to create a TreeSet

public TreeSet(SortedSet<E> s) { this(s.comparator()); // addAll elements of SortedSet s to TreeSet; }Copy the code

summary

TreeSet is implemented based on TreeMap.

Elements in a TreeSet can be sorted either naturally or by the Comparator provided when the TreeSet was created. This depends on the constructor used.

TreeSet provides guaranteed log(n) time overhead for basic operations (Add, Remove, and Contains).

TreeSet is asynchronous. Its iterator method returns iterators that are fail-fast.

Commonly used method

add

The values are the same PRESENT (Object), and the underlying map put method is called, which is called TreeMap PUT

Add it if it doesn’t exist and don’t add it if it does

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

addAll

Public Boolean addAll(Collection<? extends E> c) { // Use linear-time version if applicable if (m.size()==0 && c.size() > 0 && c instanceof SortedSet && m instanceof TreeMap) { SortedSet<? extends E> set = (SortedSet<? extends E>) c; TreeMap<E,Object> map = (TreeMap<E, Object>) m; Comparator<? super E> cc = (Comparator<? super E>) set.comparator(); Comparator<? super E> mc = map.comparator(); if (cc==mc || (cc ! = null && cc.equals(mc))) { map.addAllForTreeSet(set, PRESENT); // return true when the TreeMap method is called; } } return super.addAll(c); }Copy the code

remove

The value added before is PRESENT (Object), so the value is deleted

If you call the map delete method, the successful return value will also be PRESENT (Object), so it will be equal.

Return true, false if the deletion fails.

public boolean remove(Object o) {
        return m.remove(o)==PRESENT;
    }
Copy the code

clear

Clear all keys directly by calling map’s clear method

public void clear() {
        m.clear();
    }
Copy the code

summary

It also provides a series of methods, such as:

// Returns the sequential iterator for TreeSet. // TreeSet is implemented by TreeMap, Public Iterator<E> Iterator () {return m.avigableKeySet ().iterator(); } // Return the iterator for TreeSet in reverse order. Public Iterator<E> descendingIterator() {return m.descendingKeyset ().iterator(); }Copy the code

conclusion

1. No duplicate elements;

2. With sorting function;

3. The elements in the TreeSet must implement the Comparable interface and rewrite the compareTo() method, which the TreeSet uses to determine whether elements are duplicated and in what order they should be.

4. TreeSet can store classes defined in the Java class library directly, such as String, Integer, etc., because they already implement the Comparable interface.

5. TreeSet stores only one instance of a user-defined class unless proper processing is performed. Otherwise, you cannot determine whether the instance is duplicated.

6. Depending on TreeMap, the underlying layer is TreeMap.

7. Compared with HashSet,TreeSet has the advantage of being orderly but has the disadvantage of being slower to read.

Choose different collections for different scenarios.

TreeSet and HashSet

Similarities:

Are unique sets of sets that are not repeated.

Difference:

The underlying structure

A HashSet stores data in a Hash table, whereas a TreeSet stores data in a binary balanced tree.

The function

Since TreeSet is an ordered Set, you can use a SortedSet. Interface methods such as first() and last().

But because you want to sort, you have to affect speed.

So, using hashsets without ordering is faster than using hashsets that store data in Hash tables.

Use TreeSet if ordering is required.

TreeSet and HashMap

The same

1.TreeMap and TreeSet are both ordered sets.

2. TreeMap and TreeSet are synchronous collection, so they are not Shared between multiple threads, but you can use the method Collections. SynchroinzedMap () to achieve synchronization.

3. Both run slower than Hash sets, with an internal operation time of O(logN) on elements and O(1) for HashMap/HashSet.

The difference between

TreeSet implements the Set interface, while TreeMap implements the Map interface.

2.TreeSet stores only one object, whereas TreeMap stores two keys and values (only the keys are ordered).

3.TreeSet cannot have duplicate objects, but TreeMap can.

The words in the back

love all, trust a few, do wrong to none .

Fixation of trailer

When it comes to distribution, it is inevitable to consider distributed lock, and its implementation is particularly important.

So the next phase is the implementation of distributed locks.