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
518
votes
14 answers

Hashset vs Treeset

I've always loved trees, that nice O(n*log(n)) and the tidiness of them. However, every software engineer I've ever known has asked me pointedly why I would use a TreeSet. From a CS background, I don't think it matters all that much which you use,…
heymatthew
  • 6,716
  • 5
  • 26
  • 22
88
votes
7 answers

maintaining TreeSet sort as object changes value

I've got a object that defines a 'natural sort order' using Comparable<>. These are being stored in TreeSets. Other than removing and re-adding the object, is there another way to update the sort when the members that are used to define the sort…
Stevko
  • 4,345
  • 6
  • 39
  • 66
65
votes
5 answers

Is it possible that TreeSet equals HashSet but not HashSet equals TreeSet

I had a interview today and the person taking my interview puzzled me with his statement asking if it possible that TreeSet equals HashSet but not HashSet equals TreeSet. I said "no" but according to him the answer is "yes". How is it even possible?
user13777664
45
votes
4 answers

TreeSet giving incorrect output - Java8

While working with a tree set, I found very peculiar behavior. As per my understanding following program should print two identical lines: public class TestSet { static void test(String... args) { Set s = new…
T-Bag
  • 10,916
  • 3
  • 54
  • 118
35
votes
5 answers

How to find the index of an element in a TreeSet?

I'm using a TreeSet and I'd quite simply like to find the index of a number in the set. Is there a nice way to do this that actually makes use of the O(log(n)) complexity of binary trees? (If not, what should I do, and does anyone know why…
jtbandes
  • 115,675
  • 35
  • 233
  • 266
35
votes
6 answers

Java's TreeSet equivalent in Python?

I recently came across some Java code that simply put some strings into a Java TreeSet, implemented a distance based comparator for it, and then made its merry way into the sunset to compute a given score to solve the given problem. My…
viksit
  • 7,542
  • 9
  • 42
  • 54
34
votes
11 answers

How come Java's TreeSet has no get() method?

What if I want to retrieve and update objects that stored in a TreeSet? The reason I'm asking, is that I want to be able to maintain some data stracture that will store Students. I want it to be sorted (by grades - which is an instance variable of…
so.very.tired
  • 2,958
  • 4
  • 41
  • 69
32
votes
5 answers

Treeset to order elements in descending order

Here is the piece of code that I have used for Java 5.0 TreeSet treeSetObj = new TreeSet( Collections.reverseOrder() ) ; Collections.reverseOrder() is used to obtain a comparator in order to reverse the way the elements are stored…
Gaurav Saini
  • 744
  • 2
  • 11
  • 22
31
votes
3 answers

Converting a TreeSet to ArrayList?

I have a TreeSet which contains > 100k objects. I have another method which requires ArrayList as an param. Is there any way I can accomplish this without iterating whole TreeSet and then adding each object manually to ArrayList ?
priyank
  • 4,634
  • 11
  • 45
  • 52
28
votes
3 answers

Computational Complexity of TreeSet methods in Java

Is the computational complexity of TreeSet methods in Java, same as that of an AVLTree? Specifically, I want to know the computational complexity of the following methods: 1.add 2.remove 3.first 4.last 5. floor 6. higher Java Doc for method…
user1628340
  • 901
  • 4
  • 14
  • 27
25
votes
10 answers

TreeSet constructor with Comparator parameter

In Java’s documentation for its class TreeSet one of the constructors is shown to have the following header: TreeSet(Comparator c) Can someone help explain why there is a constructor for TreeSet which takes a comparator object as its…
TheRapture87
  • 1,403
  • 3
  • 21
  • 31
24
votes
8 answers

How to return the k-th element in TreeSet in Java?

Maybe I am not using the right data structure. I need to use a set, but also want to efficiently return the k-th smallest element. Can TreeSet in Java do this? There seems no built-in method of TreeSet to do this.
user1096734
23
votes
5 answers

Difference between NavigableSet, SortedSet and TreeSet in Java

A TreeSet puts an element in natural ordering or by the provided comparator. A SortedSet is also keeps the element in natural order But what is the difference between them and NavigableSet? Where are NavigableSets useful? Some example to show its…
eagertoLearn
  • 9,772
  • 23
  • 80
  • 122
23
votes
4 answers

Java - TreeSet and hashCode()

I have a quick question about TreeSet collections and hashCode methods. I have a TreeSet and I'm adding objects to it, before I add an object, I check to see if it exists in the TreeSet using the contains method. I have 2 distinct objects, each of…
Gaz
  • 3,855
  • 6
  • 28
  • 33
22
votes
6 answers

Why does TreeSet throw a ClassCastException?

I am trying to add two 'Employee' objects to a TreeSet: Set s = new TreeSet(); s.add(new Employee(1001)); s.add(new Employee(1002)); But it throws a ClassCastException: Exception in thread "main" java.lang.ClassCastException:…
Rais Alam
  • 6,970
  • 12
  • 53
  • 84
1
2 3
41 42