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

Can TreeSet be sorted in opposite way when objects are already in the set?

Can TreeSet be sorted in opposite way when objects are already in the set? I mean, I inserted 10^6 words in a TreeSet and I want to sort it now the opposite way. To my knowledge this is not possible, but I need to ask.
-1
votes
3 answers

How to notify a JTable to Update its Model

I'm looking for a pattern to notify my JTable that the Data which are handled in the TableModel have changed. The Idea is that I got a global singleton TreeSet managing the Data. I receive randomly updates on the TreeSet.If any changes are made to…
JonasP
  • 51
  • 1
  • 6
-2
votes
1 answer

Java Generic TreeSet

Hi I have problem with creating a generic TreeSet with its Iterator. I want to use it to three different kind of Types. Can anyone help me how to do that?
Mohammed Mohammed
  • 403
  • 1
  • 9
  • 18
-2
votes
1 answer

Why does my main sorted set treeset get modified when i modify my tailset in java?

Please refer to the doIt function. Whenever I store my tailset in a variable and perform an action on it, it also changes my ss variable's value. How do I remove the first element of my tailset without removing it from my main sortedset, ss? input…
Thara
  • 7
  • 2
-2
votes
1 answer

TreeSet.lower() Method is not returning required output while using explicit comparator constructor

I am using an explicit comparator to allow duplicates values in TreeSet. While extracting exact lower value for given value using TreeSet.lower() method I am getting output same as given value (argument inside TreeSet.lower() method) import…
ram gopal
  • 3
  • 1
-2
votes
1 answer

TreeSet object removal does not work even after implementing Comparator

I have tried to implement AStar Algorithm in Java with Eclipse. My positions in the graph are represented by Objects. I use a TreeSet to store the positions and implemented a comparator for the object specific sorting. However at one line the code…
-2
votes
1 answer

Internal working of ceiling and floor function of TreeSet in Java

I am working on a problem where I need to find the pair in which the difference of value is at most k. Now tree set in java does that for me but I am curious to know how this is working. I am assuming it creates some sort of bucket and map that…
Atul Agrawal
  • 1,474
  • 5
  • 22
  • 41
-2
votes
1 answer

What structure is underlying Java TreeSet?

Java TreeSet is a red-black tree self balancing structure. But what is the structure to store the data? Array or linked list?
arminvanbuuren
  • 957
  • 1
  • 9
  • 16
-2
votes
1 answer

Graph: TreeSet is not adding all the edges

I am working on an undirected graph problem. I have a Node class which contains nodeName and List. Then I have Edge class which holds Node start, Node end and weight. And finally I have a Graph class which has one instance variable Map
RDM
  • 1,136
  • 3
  • 28
  • 50
-2
votes
1 answer

TreeSet vs Arraylist: Modifying elements

so i want to have Chat-Elements stored in a Collections and I still don't know what Collections to use. It is just like the Chatlist of WhatsApp where it is sorted by date but properties/parameters of each chat can change anytime (chat muted/marked…
Ayox
  • 691
  • 5
  • 15
-2
votes
2 answers

How do I use a TreeSet in Java that allows Duplicates?

I want a data structure with priority queue functions in O(logn) time and also be able to delete a specific element that's not necessarily the head in O(logn) time. I heard the TreeSet in java does it but does not allow Duplicates, How do i get…
-2
votes
1 answer

I am able to enter ten entries for my TreeSet, but only the last entry prints out

The assignment asks for entry of 10 patient records, to include patientId, patientFirstName, patientLastName, patientIllness, and notes; this is to be put into a TreeSet with a Comparator that will abc by last name. This is the section of code that…
J.Speer
  • 1
  • 3
-2
votes
1 answer

How to retrieve words based on user inputting letter from a TreeSet or TreeMap efficiently without delay?

I want to store some elements in TreeMap or TreeSet and when user insert an alphabet based on that first word starts from that alphabet should be returned. How to retrieve words based on user inputting letter from a TreeSet or TreeMap efficiently…
Mujthaba Ibrahim
  • 207
  • 3
  • 16
-2
votes
1 answer

Unexpected type TreeSet Java

In this line public TreeSet storage = new TreeSet(); I get the following error when I try to compile: error: unexpected type public TreeSet storage = new TreeSet(); What do I need to do to fix this?
chef
  • 21
  • 2
-2
votes
1 answer

Using Iterator for TreeSet

I know that a TreeSet is ordered. But does that guarantee that every time I create an iterator, it is going to go through the elements of the collection in the same order (provided that I am not adding/removing any elements)?
Mackiavelli
  • 432
  • 2
  • 12
1 2 3
41
42