Questions tagged [treeset]

a Java set implementation sorting its items upon insertion; provided by JRE/JDK.

TreeSet implements interface NavigableSet which is a sub-interface of SortedSet. So in addition to being a Set - i.e. a Collection containing unique entries - a TreeSet sorts its entries either by their natural order if they are Comparable or by an explicit Comparator provided as a constructor parameter.

The class name originates in the fact that a TreeSet is backed by a TreeMap which in turn implements a red-black tree (a self-balancing binary search tree) to order its elements.

Please note that elements are implicitly assumed to be immutable, i.e. their order is never updated after insertion. This makes updating entries in a TreeSet tricky, because it only works in this specific order:

  1. Remove element from TreeSet
  2. Modify element properties
  3. Re-add element to TreeSet

Refrain from modifying entries before removal, because a modified entry might not even be found in the set anymore after modification.

Please also note that this kind of remove or update operation should never be done while iterating over the set because it would lead to undefined Iterator behaviour. So you either need to iterate over a copy of the set or use an advanced 3rd party implementation like the UpdateableTreeSet provided by Alexander Kriegisch.

627 questions
21
votes
1 answer

Why doesn't removing from a TreeSet with a custom comparator remove a larger set of items?

Using both Java 8 and Java 11, consider the following TreeSet with a String::compareToIgnoreCase comparator: final Set languages = new…
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
19
votes
2 answers

TreeSet vs Java 8 Streams performance

Which way is the most efficient to process a distinct and sorted collection? 1. Enhanced loop with TreeSet Set ret = new TreeSet<>(); for (Foo foo : foos) ret.add(new MyObj(foo)); 2. Simple Stream List ret =…
Guillaume F.
  • 5,905
  • 2
  • 31
  • 59
19
votes
4 answers

Migrating Java TreeMap code to Scala?

I am migrating my Java code base to pure Scala and I am stuck on this one piece of code. I have an implementation of an IntervalMap i.e. a data structures that let's you efficiently map ranges [from,to] to values where the set, delete and get…
pathikrit
  • 32,469
  • 37
  • 142
  • 221
19
votes
5 answers

What are the pros and cons of a TreeSet

Just wondering what the pros and cons of a TreeSet is, if anyone could tell me please? Thanks!
Rifk
  • 289
  • 1
  • 2
  • 8
16
votes
2 answers

Creating a TreeSet with a non-Comparable class: why a run-time exception, rather than compile-time error?

If I create an arbitrary class that does not implement Comparable, and try to use it as a treeset, it throws an exception at run time when an object is inserted: public class Foo { } public TreeSet fooSet = new TreeSet(); fooSet.add(new…
uscjeremy
  • 348
  • 1
  • 5
  • 12
14
votes
5 answers

java TreeSet - don't remove duplicate items

TreeSet removes different items with the same Comprator value. I don't want it be removed. Is there any way to control this? Or use another container class? Added: OK. It seems I can't use Set. I need insert sorting feature, for performance…
pengguang001
  • 4,045
  • 6
  • 27
  • 31
13
votes
6 answers

Keeping mutable objects sorted in TreeSets at all times

It came to my notice that a TreeSet doesn't keep the mutable objects in sorted order if object attribute values are changed later on. For example, public class Wrap { static TreeSet ts = new TreeSet(new Comparator(){ …
aps
  • 2,452
  • 10
  • 35
  • 47
13
votes
7 answers

Using iterator on a TreeSet

SITUATION: I have a TreeSet of custom Objects and I have also used a custom Comparator. I have created an iterator to use on this TreeSet. TreeSet ts=new TreeSet(); Iterator itr=ts.iterator(); while(itr.hasNext()){ …
aps
  • 2,452
  • 10
  • 35
  • 47
13
votes
4 answers

In TreeSet, Sorting & Uniqueness of custom objects based on different properties

Below is my Student class class Student implements Comparable { String name; int rollNo; @Override public int compareTo(Object obj) { return ((Student)obj).name.compareTo(this.name); } } latest modification: but still no…
SmartSolution
  • 2,320
  • 5
  • 37
  • 49
13
votes
7 answers

Adding null to empty TreeSet raising NullPointerException

import java.util.TreeSet; class Test { public static void main(String[] args) { TreeSet t=new TreeSet(); t.add(null); System.out.println(t); } } output: NullPointerException. I read in many articles that empty…
user3516780
  • 149
  • 1
  • 1
  • 5
13
votes
8 answers

A TreeSet or TreeMap that allow duplicates

I need a Collection that sorts the element, but does not removes the duplicates. I have gone for a TreeSet, since TreeSet actually adds the values to a backed TreeMap: public boolean add(E e) { return m.put(e, PRESENT)==null; } And the TreeMap…
Zeeshan
  • 11,851
  • 21
  • 73
  • 98
12
votes
5 answers

When do you know when to use a TreeSet or LinkedList?

What are the advantages of each structure? In my program I will be performing these steps and I was wondering which data structure above I should be using: Taking in an unsorted array and adding them to a sorted structure1. Traversing through…
Enf
  • 129
  • 1
  • 1
  • 3
12
votes
4 answers

TreeSet: number of elements less than a value efficiently

I need a way to calculate the number of elements less than X in a TreeSet of Integers really fast. I can use the subSet() headSet() tailSet() methods but they are really slow (I just need the count, not the numbers themselves). Is there a…
mnmp
  • 380
  • 2
  • 15
12
votes
3 answers

Treeset.contains() problem

So I've been struggling with a problem for a while now, figured I might as well ask for help here. I'm adding Ticket objects to a TreeSet, Ticket implements Comparable and has overridden equals(), hashCode() and CompareTo() methods. I need to check…
Jasper
  • 185
  • 1
  • 2
  • 7
11
votes
11 answers

Comparator and equals()

Suppose I need TreeSet with elements sorted with some domain logic. By this logic it doesn't matter order of some elements that doesn't equal so compare method can return 0, but in this case I couldn't put them in TreeSet. So, question: what…
Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132
1
2
3
41 42