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
4
votes
1 answer

Iterator with starting point in iterator within treeset

I have a TreeSet and iterate through it. When iterating through it I have to compare each element with the remaining entries in this TreeSet. The problme is that I can't have an iterator which starts at a specific point. TreeSet tree = new…
arc
  • 4,553
  • 5
  • 34
  • 43
4
votes
1 answer

GSON: Serialize object with java.util.TreeSet

How can I serialize a TreeSet properly? In order to give you an idea of what's not working I've set up this little demo project. The main goal is to print a JSON string of my QData object. App.java package de.company.gsonserializer; import…
Erando
  • 811
  • 3
  • 13
  • 27
4
votes
2 answers

TreeSet contains/remove not working?

I am keeping Node objects in a TreeSet: public TreeSet viewNodes = new TreeSet(); Node looks like this: public class Node implements Comparable{ private long nodeID; ... public long getID() { return nodeID; …
Casey
  • 41
  • 4
4
votes
4 answers

Using HashMap Value to remove object from TreeSet

I have a HashMap where key is a character and value is some user-defined object. I am adding same objects in the TreeSet. The number of entries in HashMap and TreeSet are equal. Later on I want to retrieve a object from HashMap using user supplied…
Curious
  • 65
  • 10
4
votes
4 answers

TreeSet doesn't work, what's wrong with the following code?

The following code should print 3 persons, but it actually print 4 persons, why? Person("a", 1) and Person("a", 4) should be treated as the same, but they are not. import java.util.TreeSet; public class Person implements Comparable{ …
4
votes
1 answer

TreeSet.contains() does not call overwritten equals

i have a problem with the contains() method of TreeSet. As I understand it, contains() should call equals() of the contained Objects as the javadoc says: boolean java.util.TreeSet.contains(Object o): Returns true if this set contains the…
4
votes
1 answer

Treeset alphabetical sorting

How do I get the treeset to sort alphabetically? And remove duplicates.. it's been driving me nuts for a day. Maybe I need to get some sleep.. public static void main(String[] args) { String fileName = args[0]; String words; …
A C
  • 417
  • 3
  • 5
  • 17
4
votes
5 answers

Why do we need a TreeMap/TreeSet when we have SortedMap/SortedSet?

Ok so SortedMap / SortedSet is an interface, and TreeMap / TreeSet is it's implementation. Both of them keep the elements in sorted order, right? So why do we need TreeMap / TreeSet?
user2027425
  • 389
  • 3
  • 5
  • 14
4
votes
2 answers

How Iterator implement in TreeSet

I read java source about TreeSet , but I can not find the implement of Iterator in TreeSet. Could anybody tell me how Iterator implements in TreeSet, and where is the source code in TreeSet? thanks!
Zhong Yang
  • 203
  • 1
  • 2
  • 6
4
votes
4 answers

Sort a java collection of objects by one value and remain unique by another value

I need to sort a java collection of objects by an Integer value "level". I also need to determine if this collection already contains an object by "title". I believe the best choice of a collection is a TreeSet to have an ordered set of unique…
Atma
  • 29,141
  • 56
  • 198
  • 299
3
votes
2 answers

Java arbitrary comparator on Object

Let's say I am building a TreeSet of an object for which the ordering depends on only one value. I cannot do TreeSet tree = new TreeSet<>(Comparator.comparingInt(Foo::getX)); because if I add two different Foo objects with the same x then one…
Ricola
  • 2,621
  • 12
  • 22
3
votes
1 answer

Java Comparable and TreeSet

Why do objects put in a TreeSet have to implement the Comparable interface? I'm trying to create a TreeSet containing some objects of my own class but ClassCastException is thrown when I try to add objects in the set.
asim
  • 73
  • 5
3
votes
5 answers

Java Comparator for InetSocketAddress

I need to write Comparator for InetSocketAddress so I can use this class in a TreeSet. They need to be comparable by address and port. The code would look something like this, but the problem is I don't know how to compare addresses and ports by…
BlackCat
  • 329
  • 5
  • 17
3
votes
1 answer

Counting Inversions using TreeSet Java

I'm using TreeSet for solving counting inversions problem. I'm using following approach which uses gnu_pbds which works in O(logn) time. Algorithm Insert the first element of the array in the Ordered_Set. For all the remaining element in arr[] do…
user8838577
3
votes
2 answers

What is the time complexity of getting the headSet of a TreeSet in Java? Also, what if I call the headSet method 'n' times?

I was doing a hackerrank question that requires me to find out the number of shifts that need to occur to sort an array using Insertion Sort without actually sorting the array with insertion sort because otherwise that would be O(n^2)…