Home Java javaTutorial How to implement TreeSet in Java? (detailed explanation)

How to implement TreeSet in Java? (detailed explanation)

Nov 27, 2018 pm 05:08 PM
treeset

#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
Copy after login

  1. Unsurprisingly, it inherits the abstract class AbstractSet, which facilitates expansion;

  2. implements a NavigableSet interface, which is similar to the NavigableMap interface and provides various navigation Method;

  3. implements the Cloneable interface and can be cloned;

  4. implements the Serializable interface and can be serialized;

Here we mainly look at the NavigableSet interface class:

public interface NavigableSet<E> extends SortedSet<E>
Copy after login

Familiar taste, inherit the SortedSet interface. SortedSet provides a method that returns a comparator:

Comparator<? super E> comparator();
Copy after login

, 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
Copy after login

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;
    }
Copy after login

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); // 一个一个添加元素
    }
Copy after login

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);
    }
Copy after login

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) {
        }
    }
Copy after login

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());
    }
......
Copy after login

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);
        }
Copy after login

TreeSet also supports reverse order output, because there is an implementation of descendingIterator:

public Iterator<E> descendingIterator() {
        return m.descendingKeySet().iterator();
    }
Copy after login

Summary

  1. ##TreeSet is implemented based on TreeMap, supports natural sorting and custom sorting, and can output in reverse order;

  2. TreeSet does not allow null values;

  3. 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Mar 07, 2025 pm 06:09 PM

This article analyzes the top four JavaScript frameworks (React, Angular, Vue, Svelte) in 2025, comparing their performance, scalability, and future prospects. While all remain dominant due to strong communities and ecosystems, their relative popul

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed Mar 07, 2025 pm 05:52 PM

This article addresses the CVE-2022-1471 vulnerability in SnakeYAML, a critical flaw allowing remote code execution. It details how upgrading Spring Boot applications to SnakeYAML 1.33 or later mitigates this risk, emphasizing that dependency updat

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

Node.js 20: Key Performance Boosts and New Features Node.js 20: Key Performance Boosts and New Features Mar 07, 2025 pm 06:12 PM

Node.js 20 significantly enhances performance via V8 engine improvements, notably faster garbage collection and I/O. New features include better WebAssembly support and refined debugging tools, boosting developer productivity and application speed.

How does Java's classloading mechanism work, including different classloaders and their delegation models? How does Java's classloading mechanism work, including different classloaders and their delegation models? Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

Iceberg: The Future of Data Lake Tables Iceberg: The Future of Data Lake Tables Mar 07, 2025 pm 06:31 PM

Iceberg, an open table format for large analytical datasets, improves data lake performance and scalability. It addresses limitations of Parquet/ORC through internal metadata management, enabling efficient schema evolution, time travel, concurrent w

How to Share Data Between Steps in Cucumber How to Share Data Between Steps in Cucumber Mar 07, 2025 pm 05:55 PM

This article explores methods for sharing data between Cucumber steps, comparing scenario context, global variables, argument passing, and data structures. It emphasizes best practices for maintainability, including concise context use, descriptive

How can I implement functional programming techniques in Java? How can I implement functional programming techniques in Java? Mar 11, 2025 pm 05:51 PM

This article explores integrating functional programming into Java using lambda expressions, Streams API, method references, and Optional. It highlights benefits like improved code readability and maintainability through conciseness and immutability

See all articles