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

Key missing in TreeSet

I'm adding the hashMap key to one by one in treeSet and try to sort them based on comparator passed in lambda in treeSet. Without comparator, it is working fine based on default TreeSet. String s = "tree"; Map freqMap =…
3
votes
6 answers

java: Comparator and Treeset to remove duplicates

i have a java class like this public class A { private String field1; private String field2; // getters, setters but no equals and hashcode } and a list of objects of this class, i want to remove from this list all the duplicates…
res1
  • 3,482
  • 5
  • 29
  • 50
3
votes
1 answer

Concurrent access to a TreeSet doesn't seem to work

I'm creating a Java class IdGenerator that allocates a unique integer ID each time one is requested. It uses a TreeSet to store ranges of free IDs, and each time an ID is requested it looks in the set to find a range, allocates the first ID in the…
Jeremy Hicks
  • 121
  • 6
3
votes
1 answer

TreeSet Comparator failed to remove duplicates in some cases?

I have the following comparator for my TreeSet: public class Obj { public int id; public String value; public Obj(int id, String value) { this.id = id; this.value = value; } public String toString() { …
user1589188
  • 5,316
  • 17
  • 67
  • 130
3
votes
1 answer

How Java TreeSet are implemented

I was wondering how a TreeSet is implemented in Java. In fact, I was wondering, is a TreeSet using, under the hood, a balancing tree, or is it using an array ? In other word, when I am adding an element to my treeSet, I know it will be ordered, but…
David Lefaivre
  • 191
  • 3
  • 14
3
votes
1 answer

Iterator over TreeSet causes infinite loop

For this assignment, I'm required to save instances of a custom data class (called User) each containing 2 strings into a TreeSet. I must then search the TreeSet I created for a string taken from each line of another file. The first file is a .csv…
3
votes
1 answer

algorithm about sliding window

This question is a variant of k empty slot from leetcode. the new question is, ask to find the last day when there are k consecutive bloomed flowers. e.g. total 7 days, 1 represents flower bloomed,0 represents flower not bloomed, k=3 day1:1 0 0 0 0…
Tianbo
  • 67
  • 2
  • 11
3
votes
1 answer

How do I return a TreeSet of the values contained in a TreeMap?

I am trying to get a set of all the values for keys in a TreeMap that are greater than some value, key. Code attempt below: TreeSet set = (TreeSet)tMap.tailMap(key, false).values(); Is there a way to do this in Java? Thanks!!
Clemson
  • 478
  • 5
  • 20
3
votes
1 answer

reverse sort of treeset with keeping sort on each level

Im just learning Collections and I have a task. Some organization want to create a cataloge of departments. Codes of departments are array of Strings: “K1\SK1” “K1\SK2” “K1\SK1\SSK1” “K1\SK1\SSK2” “K2” “K2\SK1\SSK1” …
mironec
  • 31
  • 5
3
votes
2 answers

Ranking elements in TreeSet

I know a java treeset can't have identical elements and so I have to somehow differentiate an element from another even if they have the same "value". I want to be able to rank the elements and I'm noticing an interesting behavior. TreeSet
JPC
  • 8,096
  • 22
  • 77
  • 110
3
votes
3 answers

How to find the rank of an element in a TreeSet

I know you can find the first and last elements in a treeset. What if I wanted to know what the second or third element was without iterating? Or, more preferable, given an element, figure out it's rank in the treeset. Thanks EDIT: I think you…
JPC
  • 8,096
  • 22
  • 77
  • 110
3
votes
2 answers

Java TreeSet method that controls insertion of element into Red-Black tree

I really want to deepen my understanding of exactly how a TreeSet, particularly the no-argument version that does not use a comparator, maintains the elements that it contains in order. I cannot find a satisfactory explanation anywhere; they are…
Wes1324
  • 1,077
  • 8
  • 13
3
votes
3 answers

Java hashset and treeset

I have a question. It says that hashset in java doesn't mantain an order but looking at my program public static void main(String[] args) { HashSet io=new HashSet(); Integer io1=new Integer(4); Integer io2=new…
User124235
  • 147
  • 8
3
votes
4 answers

TreeSet in Java

I understand Java best practice suggests that while declaring a variable the generic set interface is used to declare on the left and the specific implementation on the right. Thus if I have to declare the Set interfaces, the right way…
Zeus
  • 2,213
  • 8
  • 28
  • 45
3
votes
2 answers

why use \0 to include highEndPoint as part of the sublist

I saw the code below from java tutorial oracle. In order to count the number of words between doorbell (inclusive) and pickle(inclusive), the author added \0 after the word pickle. I understand that the effect of adding \0 after pickle, is that the…
user5849987