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
2
votes
4 answers

TreeSet contains method doesn't work for me

I want to put the custom's data into a TreeSet. When the custom number is same, I add the volume of trade. Here is my TradeNode class that implements the Comparable Interator. import java.util.Comparator; public class TradeNode implements…
nhr
  • 23
  • 5
2
votes
2 answers

How to implement a sorted table (order by a field of the element) by using java TreeSet?

I used TreeSet for this and it works in a per snapshot style. In other words, sort once displays once. Now, I want to implement a realtime sorted table. Whenever there is a value change in any elements, the sorted table will be updated…
Ken Tsang
  • 61
  • 5
2
votes
2 answers

Why is subSet method specified in SortedSet Interface instead of Set?

Why is subSet method specified in SortedSet Interface instead of Set unlike subList method of List interface?
Durlabh Sharma
  • 397
  • 2
  • 14
2
votes
4 answers

How to get the last element of a HashSet?

I want to get last element of HashSet. So there is a last() method in TreeSet so, I thought to convert set to TreeSet and apply last() method on it so that I will get last element. I tried below code : Set set= new…
bharath varma
  • 61
  • 1
  • 7
2
votes
2 answers

Problem with compareTo and TreeSet

I'm having a problem with TreeSets removing a unit from a game I'm working on. I'm making a tower defense game and the path is broken up into different blocks of a set length. The blocks know the units within it and the next block on the path. …
Jonathan
  • 77
  • 1
  • 5
2
votes
3 answers

Custom String Length Comparator: what's my mistake?

I defined a custom comparator to sort the name(String) variable of my objects by length. Here's the code from my person class: class MyNameLengthCompare implements Comparator { @Override public int compare(Person a, Person…
LuxuryMode
  • 33,401
  • 34
  • 117
  • 188
2
votes
0 answers

Deleting elements from a TreeSet with own Comparator, auto-sorting in TreeSet

I have a class A whose instances have 8 attributes, the last two are of the type "int". I have implemented the Comparator interface, where elements have to be compared according to the 7. attribute. If values of the 7. attribute are equal, object…
jupiter_jazz
  • 333
  • 2
  • 8
2
votes
2 answers

Why using custom Comparator to treeSet is breaking the equality of String objects?

I have written a comparator which is calculating consonants in a given string to compare 2 strings, but it is somehow making TreeSet think that 2 strings are equal (may be when they are of same size). public class SortDiff { public static void…
Rishi
  • 1,646
  • 2
  • 15
  • 34
2
votes
1 answer

Java iterate on a TreeMap/TreeSet in O(N) and remove other elements than current one

Is there a way to iterate through a TreeSet/TreeMap in O(N) while being able to remove an other entry than the one the iterator currently points to? Here is a simple example using a TreeSet. The set contains positive integers and I check if it's…
Ricola
  • 2,621
  • 12
  • 22
2
votes
2 answers

Why don't I see all variables in debugger?

Hi all! I tried to debug TreeSet/TreeMap but found that debugger for some reason doesn't want to show me parent and left/right children of the nodes. I expected to see nested nodes but only key/values are exposed. Of course, if I…
Pasha
  • 1,768
  • 6
  • 22
  • 43
2
votes
2 answers

Spell Checker in Java, file open but no output

I got this code for a spell Checker. It doesn't read the words.txt file. It only opens the dialog box to choose a file. When I choose the words.txt file, the dialog box closes and nothing happens. I am not sure what is wrong with this code. I keep…
Lila
  • 21
  • 4
2
votes
4 answers

Sorting tree sets without removing duplicates

myData{ itemId, location, ExpDtTm, userId } I trying to sort a TreeSet like so: TreeSet sorted = new TreeSet<>(Comparator.comparing(myData -> myData.ExpDtTm)); But there is a problem if any of the dates are equivalent they wont be…
TOTOROCATBUS
  • 172
  • 1
  • 2
  • 17
2
votes
1 answer

Treeset doesn't remove object - Java

I'm using Treeset in my project to store an Object class (that i've created early). I've also implemented build() method that have to add in my treeset the Object and it works great. Now I' ve to implement demolish() method (it has to remove the…
2
votes
1 answer

TreeMap sorted by value error

I want to sort my treemap by its value, I use a set> to do this, here is my code: StringBuilder sb = new StringBuilder(); Map map = new TreeMap<>(); for (char c : s.toCharArray()){ map.put(c,…
Keith
  • 53
  • 1
  • 2
  • 8
2
votes
2 answers

TreeSet to find k most frequent words in a book?

The commonly occurring question of finding k most frequent words in a book ,(words can dynamically be added), is usually solved using combination of trie and heap. However, I think even using a TreeSet should suffice and be cleaner with log(n)…
Manas Saxena
  • 2,171
  • 6
  • 39
  • 58