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
7
votes
3 answers

How to retrieve elements from sorted TreeSet using Binary Search?

I am trying to merge multiple sorted lists into one TreeSet.. And then I am thinking to apply Binary Search algorithm on that TreeSet to retrieve the element in O(log n) time complexity.. Below is my code in which I am passing List of Lists in in…
user2467545
6
votes
2 answers

Data lost from TreeSet when using Comparator

I have the following code which will sort the Employees's based on their experience. I am adding 2 employees with different name and same experience. I am expecting that at the end set will have 2 employees, but I am getting only one. I have also…
Jobin
  • 5,610
  • 5
  • 38
  • 53
6
votes
2 answers

Why adding null in HashSet does not throw Exception,but adding null in TreeSet throw Exception

Why adding null in HashSet does not throw Exception,but adding null in TreeSet throw Exception. Set s = new TreeSet(); s.add(null); throws NullPointerException Set s = new HashSet(); Allow Null value to be…
V.S
  • 165
  • 1
  • 1
  • 9
6
votes
3 answers

How to add to SortedSet items from an Array?

I have a SortedSet defined this way: SortedSet messageCollection = new TreeSet(new Comp()); and I have an array of RatedMessage[] I had to use the array as the set misses the serialization feature, now I need to…
Pentium10
  • 204,586
  • 122
  • 423
  • 502
6
votes
5 answers

java TreeSet: comparing and equality

I'd like to have list of object sorted with property 'sort_1'. But when I want to remove I'd like it to use property 'id'. The following code represents the problem. package javaapplication1; import java.util.TreeSet; public class MyObj implements…
knocker_d
  • 506
  • 6
  • 16
6
votes
3 answers

HashSet and TreeSet performance test

I read about how TreeSet is slower than HashSet, (that adding elements into a TreeSet is slower) so i made a performance test, i'm trying to find out if it's better to add elements to HashSet and then move them into a TreeSet or put them in there in…
Peter
  • 409
  • 2
  • 6
  • 14
6
votes
3 answers

TreeSet add() returning false

I'm implementing an A* algorithm in Java, and I'm using a TreeSet as an easy way of keeping the open list sorted. If you're not familiar with A*, it's basically a function to get the shortest path from A to B, and the open list is a list of nodes…
divillysausages
  • 7,883
  • 3
  • 25
  • 39
6
votes
1 answer

HashSet removes duplicates but TreeSet does not?

Output of below class is : size is 3 size is 1 But if I change the TreeSet to a HashSet so line : Set set = new TreeSet(); becomes Set set = new…
blue-sky
  • 51,962
  • 152
  • 427
  • 752
6
votes
1 answer

java treeset throwing illegalArgumentException: key out of range

I've stripped down the code to reproduce an example which throws the error: public class Test { public static void main(String[] args) { NavigableSet set = new TreeSet( Arrays.asList("a", "b", "c", "d")); …
Kes115
  • 2,070
  • 2
  • 21
  • 37
6
votes
2 answers

How to print objects from a TreeSet

I want to print the instance variables of the objects I stored in my TreeSet. So given an Object with three instance variables, I want to iterate over the Objects in my TreeSet and print their ivars but: while ( iter.hasNext() ) { …
kwoebber
  • 425
  • 2
  • 5
  • 8
5
votes
1 answer

how to check a collection of rectangles for holes and intersctions?

I am searching for a way to check a collection (Java TreeSet) of rectangles - implemented by a "comparable" Java class using google guavas Range for x and y range - for intersections and holes. i know that an option could be to use kd-trees, but I…
dermoritz
  • 12,519
  • 25
  • 97
  • 185
5
votes
3 answers

TreeSet instance in Set reference variable

For the code: Set set = new TreeSet<>(); set.add(new Phone("Harry")); I get error: Phone cannot be cast to java.base/java.lang.Comparable Why Phone has to implement Comparable if reference variable is type Set? If the reference variable…
mislav23
  • 83
  • 5
5
votes
1 answer

How to assign an order for TreeSet in Scala without repeating myself

I have this segment of Scala code which defines an ordering and applies it to a TreeSet. This part compiles fine. val acctOrdering = new Ordering[Account] { def compare(acc1: Account, acc2: Account) { // code to compare based on various…
Gigatron
  • 1,995
  • 6
  • 20
  • 27
5
votes
1 answer

C++ treeset implementation with templates

I have to write a template for a treeset. The size of a leaf is 0. When the create_empty_set() is called it should make a leaf, when you add T data the leaf should become a branch and its value should be put in the left or right. No duplicates are…
Kelvijn
  • 179
  • 4
  • 17
5
votes
5 answers

Trimming a sorted set

I have a SortedSet (specifically a TreeSet) containing updates. An update is something like an SVN commit, Facebook wall post, new Trac ticket, etc. I'm storing these in a SortedSet because: Sorted: The updates need to be sorted by date,…
Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301