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

TreeSet only adding one value?

I am new to Java Collections. I have three classes namely MyData, Initializer and SetBasics I have created a HashSet and a TreeSet. Now the problem is HashSet is working fine but TreeSet is only storing one value. Here are my classes Class…
0o'-Varun-'o0
  • 735
  • 1
  • 5
  • 22
3
votes
3 answers

sort arraylist into tree recursively

I have an arraylist of Object. But I need a tree with the following parameters: Some Objects are not supposed to have children (non-category objects) Category Objects are supposed to have children (which can be additional category objects, or…
CQM
  • 42,592
  • 75
  • 224
  • 366
3
votes
2 answers

Java: TreeSet compareTo method triggered at first add call

The following java code utilizes a TreeSet to collect objects derived from the class MyClass which doesn't implement the Comparable interface nor have a compareTo() method to perform the comparison between objects. By not providing that comparison…
thanos.a
  • 2,246
  • 3
  • 33
  • 29
3
votes
3 answers

I am able to insert duplicate entries in TreeSet. How to overcome this

I have a class called Employee which has employeeName and employeeId as its member variables.I am creating new Employee objects and then adding it into a TreeSet where I want to sort it based on the employeeId. But I consider 2 Employee objects to…
Prasad
  • 368
  • 1
  • 6
  • 16
3
votes
4 answers

How to pass to method TreeSet or HashMap and show it?

I have such method: public void show(Object o) { //show content of object } and I would like to pass to it TreeSet or HashMap. But its a different iteration, I mean, when I want to show a content of TreeSet, I use that code: while (…
mazix
  • 2,540
  • 8
  • 39
  • 56
3
votes
3 answers

TreeSet example

Why the 3rd object is not being added to the treeset here though it is a different one? import java.util.*; class Student implements Comparable{ public String fn,ln; public Student(String fn,String ln){ this.fn=fn; …
Girish
  • 167
  • 3
  • 3
  • 15
3
votes
2 answers

What is the time complexity of the tailSet operation of java.util.TreeSet?

I was implementing the 2D closest pair algorithm using sweepline, and it says that you need to find the six points above a certain y coordinate. What I did is I put the points in a TreeSet sorted by y-coordinate, and used the tailSet method to get…
2
votes
2 answers

TreeSet comparator based on mutable attribute

My problem is very basic, but I have no clue how to solve it correctly. I have a TreeSet which uses a comparator based on the name of the entity. However, I can change that name. How do I force a reordering of the TreeSet? TreeSet set =…
parasietje
  • 1,529
  • 8
  • 36
2
votes
1 answer

CS Mines Flood Fill problem - Java solution times out while Cpp and python succeeds (Performance improvement)

Working to solve this problem https://mines20.kattis.com/problems/mines20.paintbucket Java solution using TreeSet times out with time limit of less than 3 seconds public class PaintBucketCSMinesSolution { public static void main(String[] args)…
Mandeep Singh
  • 174
  • 1
  • 8
2
votes
3 answers

What's a more efficient alternative than converting a SortedSet to a Vector in Java?

I'm writing a contact book application in Java. The Contacts are displayed on a JList which uses a Sorted TreeSet list model. I've added a search field and I've added a key listener to it. With each key entered, the subset function of the list…
W.K.S
  • 9,787
  • 15
  • 75
  • 122
2
votes
1 answer

A bug that is not present in debug mode, but just when i run the project

i'm working on a school project, we're building an Electronic Grade book, it's just a project to learn better OOP and UML nothing of real. And we got blocked in a strange bug we worked for a lot of time trying to solve it, but we couldn't find a…
MRn0b0dy
  • 25
  • 7
2
votes
0 answers

TreeSet "higher()" returns null when it shouldn't?

I'm currently working on a project for which I use a TreeSet. The set is sorted based on a value. At some point in time, I want to get the right neighbour of a certain "key" value, which does exist and shows up in the debug with the correct values,…
Matthias K.
  • 43
  • 11
2
votes
2 answers

Java: finding previous and next value of a navigable set

I have a navigable set right now that stores Integers, how could I find the previous and next elements from a specific element? I'm not sure how to use an iterator if that's how you could find those elements. Example: NavigableSet values =…
Anan Mao
  • 47
  • 4
2
votes
2 answers

Java Set removes "complex object"

I'm always confused about the Java Collections (set, map) remove "complex object", which I mean some self-defined class rather than just primitive type. I'm experimenting like: public class Main { public static void main(String[] args) { …
LookIntoEast
  • 8,048
  • 18
  • 64
  • 92
2
votes
1 answer

TreeSet implementation?

I tried to implement a TreeSet instead of a ArrayList due to the fact that I need to sort that list. The original code was: private final List reports = new ArrayList(); public void receiveReport(final Report report) { …
ilie alexandru
  • 207
  • 2
  • 9