#The content of this article is about how to implement TreeSet in Java? (Detailed explanation) has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
HashSet is implemented based on HashMap, so how is TreeSet implemented? That's right! As everyone thinks, it is implemented based on TreeMap. Therefore, the source code of TreeSet is also very simple. The main thing is to understand TreeMap.
The inheritance relationship of TreeSet
As usual, let’s look at the inheritance relationship of the TreeSet class first:
public class TreeSet<E> extends AbstractSet<E> implements NavigableSet<E>, Cloneable, java.io.Serializable
Unsurprisingly, it inherits the abstract class AbstractSet, which facilitates expansion;
implements a NavigableSet interface, which is similar to the NavigableMap interface and provides various navigation Method;
implements the Cloneable interface and can be cloned;
implements the Serializable interface and can be serialized;
Here we mainly look at the NavigableSet interface class:
public interface NavigableSet<E> extends SortedSet<E>
Familiar taste, inherit the SortedSet interface. SortedSet provides a method that returns a comparator:
Comparator<? super E> comparator();
, like SortedMap, supports natural sorting and custom sorting. Natural sorting requires the elements added to the Set to implement the Comparable interface, and custom sorting requires implementing a Comparator.
Source code analysis
Key point
The key point is naturally how TreeSet ensures that the elements are not repeated and the elements are ordered. As mentioned earlier, it is based on TreeMap implements it, so let’s take a look.
private transient NavigableMap<E,Object> m; // 保证有序 // Dummy value to associate with an Object in the backing Map private static final Object PRESENT = new Object(); // 固定Value
Looking at the TreeSet source code, we found that there are only these two attributes (there is also a uid, which is not included here). Obviously, m is used to save elements, but m declares NavigableMap instead of TreeMap. It can be guessed that TreeMap should be instantiated in the construction method. Using NavigableMap here can make TreeSet more flexible. PRESENT has the same function as PRESENT in HashSet, it serves as a placeholder for a fixed Value value.
Look at the add and remove methods again:
public boolean add(E e) { return m.put(e, PRESENT)==null; } public boolean remove(Object o) { return m.remove(o)==PRESENT; }
is the same as the implementation of HashSet, and also uses the Key-Value key value pair saved by Map. Characteristics that will be repeated.
Constructor
Sure enough, the TreeMap in TreeSet is initialized in the constructor.
public TreeSet() { this(new TreeMap<>()); // 默认自然排序的TreeMap } public TreeSet(Comparator<? super E> comparator) { this(new TreeMap<>(comparator)); // 自定义比较器的TreeMap } public TreeSet(Collection<? extends E> c) { this(); // 还是用的默认 addAll(c); // 将元素一个一个添加到TreeMap中 } public TreeSet(SortedSet<E> s) { this(s.comparator()); // 使用传入的SortedSet的比较器 addAll(s); // 一个一个添加元素 }
A naturally ordered TreeMap is instantiated by default. Of course, we can customize the comparator.
Track the addAll method here:
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; // 强转成TreeMap Comparator<?> cc = set.comparator(); Comparator<? super E> mc = map.comparator(); if (cc==mc || (cc != null && cc.equals(mc))) { // 要保证set和map的比较器一样 map.addAllForTreeSet(set, PRESENT); // TreeMap专门为TreeSet准备的方法 return true; } } return super.addAll(c); }
The addAllForTreeSet
method of TreeMap is called:
void addAllForTreeSet(SortedSet<? extends K> set, V defaultVal) { try { buildFromSorted(set.size(), set.iterator(), null, defaultVal); } catch (java.io.IOException | ClassNotFoundException cannotHappen) { } }
You should be familiar with buildFromSorted. It has been analyzed in the TreeMap article. This method constructs the passed set elements into a red-black tree in which the bottom node is red and the other nodes are black.
Navigation method
Since NavigableSet is implemented, various navigation methods are naturally indispensable. Their implementation is also very simple, just call the navigation method corresponding to m directly. For example:
public E first() { return m.firstKey(); // 返回第一个元素 } public E lower(E e) { return m.lowerKey(e); // 返回小于e的第一个元素 } public NavigableSet<E> headSet(E toElement, boolean inclusive) { return new TreeSet<>(m.headMap(toElement, inclusive)); // 取前几个元素构成子集 } public E pollFirst() { // 弹出第一个元素 Map.Entry<E,?> e = m.pollFirstEntry(); return (e == null) ? null : e.getKey(); } public NavigableSet<E> descendingSet() { // 倒排Set return new TreeSet<>(m.descendingMap()); } ......
What needs to be noted here is the method that returns a sub-set, for example: headSet. The returned subcollection can add and delete elements, but there are boundary restrictions, for example.
// 前面构造了一个存储Int的Set // 3、5、7、9 SortedSet<Integer> subSet = intSet.headSet(8); // 最大值7,超过7越界 for (Integer sub : subSet) { System.out.println(sub); } subSet.add(2); // subSet.add(8); // 越界了 subSet.remove(3); for (Integer sub : subSet) { System.out.println(sub); }
TreeSet also supports reverse order output, because there is an implementation of descendingIterator
:
public Iterator<E> descendingIterator() { return m.descendingKeySet().iterator(); }
Summary
- ##TreeSet is implemented based on TreeMap, supports natural sorting and custom sorting, and can output in reverse order;
- TreeSet does not allow null values;
- TreeSet is not thread-safe. SortedSet can be used in a multi-threaded environment. s = Collections.synchronizedSortedSet(new TreeSet(.. .));
The above is the detailed content of How to implement TreeSet in Java? (detailed explanation). For more information, please follow other related articles on the PHP Chinese website!

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunondifferentoperatingsystemswithoutmodification.TheJVMcompilesJavacodeintoplatform-independentbytecode,whichittheninterpretsandexecutesonthespecificOS,abstractingawayOS

Javaispowerfulduetoitsplatformindependence,object-orientednature,richstandardlibrary,performancecapabilities,andstrongsecurityfeatures.1)PlatformindependenceallowsapplicationstorunonanydevicesupportingJava.2)Object-orientedprogrammingpromotesmodulara

The top Java functions include: 1) object-oriented programming, supporting polymorphism, improving code flexibility and maintainability; 2) exception handling mechanism, improving code robustness through try-catch-finally blocks; 3) garbage collection, simplifying memory management; 4) generics, enhancing type safety; 5) ambda expressions and functional programming to make the code more concise and expressive; 6) rich standard libraries, providing optimized data structures and algorithms.

JavaisnotentirelyplatformindependentduetoJVMvariationsandnativecodeintegration,butitlargelyupholdsitsWORApromise.1)JavacompilestobytecoderunbytheJVM,allowingcross-platformexecution.2)However,eachplatformrequiresaspecificJVM,anddifferencesinJVMimpleme

TheJavaVirtualMachine(JVM)isanabstractcomputingmachinecrucialforJavaexecutionasitrunsJavabytecode,enablingthe"writeonce,runanywhere"capability.TheJVM'skeycomponentsinclude:1)ClassLoader,whichloads,links,andinitializesclasses;2)RuntimeDataAr

Javaremainsagoodlanguageduetoitscontinuousevolutionandrobustecosystem.1)Lambdaexpressionsenhancecodereadabilityandenablefunctionalprogramming.2)Streamsallowforefficientdataprocessing,particularlywithlargedatasets.3)ThemodularsystemintroducedinJava9im

Javaisgreatduetoitsplatformindependence,robustOOPsupport,extensivelibraries,andstrongcommunity.1)PlatformindependenceviaJVMallowscodetorunonvariousplatforms.2)OOPfeatureslikeencapsulation,inheritance,andpolymorphismenablemodularandscalablecode.3)Rich

The five major features of Java are polymorphism, Lambda expressions, StreamsAPI, generics and exception handling. 1. Polymorphism allows objects of different classes to be used as objects of common base classes. 2. Lambda expressions make the code more concise, especially suitable for handling collections and streams. 3.StreamsAPI efficiently processes large data sets and supports declarative operations. 4. Generics provide type safety and reusability, and type errors are caught during compilation. 5. Exception handling helps handle errors elegantly and write reliable software.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)
