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

TreeSet in java in replacement of min hip for dijikstra algorithm

Since Java priority queue does not allow to update value of element in priority queue therefore I decided to use TreeSet in Java as an alternate of priority queue to implement Dijikstra shortest distance algorithm. Since I can find element in…
-2
votes
3 answers

error in the method of TreeSet

I am using jdk7 and eclipse juno.I am learning TreeSet.while i am making a basic program of TreeSet it gives me error.I crosschecked with my booklet but i didnot found any change ,but my program is giving me error. This is my program import…
-3
votes
2 answers

Anomalous behaviour with TreeSet() with custom comparator

What I've done in the below program is, I've created a TreeSet named "alls" with a custom comparator which compares two list and if they are equal it returns 0 else returns 1. But if I add two same list into TreeSet then TreeSet accepts two same…
ALLAN
  • 71
  • 6
-3
votes
2 answers

Java TreeSet BigO complexity seems off

I believe that Java TreeSet has a balanced tree implementation which can perform tree sort(a lot like the quick sort) quickly. My recently-encountered problem shows that it seems to be performing more comparisons that I expect. Does every balanced…
Johnny
  • 387
  • 2
  • 12
-3
votes
1 answer

TreeSet Update Object Value seems to be a Bug in Implementation

I was checking tree set and notice that I am unable to update the value in Tree set for my user defined Object e.g class Emp implements Comparable { int id; String name; public Emp(int id,String name) { this.id=id; …
-3
votes
1 answer

type TreeSet does not take parameters?

package treeset; import java.util.*; public class TreeSet { public static void main(String[] args) { SortedSet herbSetOrder = new TreeSet(herbSet); System.out.println("First herb is: " + herbSetOrder.first());…
-4
votes
2 answers

Hashset ordering issue

Recently I came across this String s = "9495963"; Set set = new HashSet(); for(char e : s.toCharArray()){ set.add(e); } System.out.println(set);//[3, 4, 5, 6, 9] The output i got was [3, 4, 5, 6, 9], so, if HashSet doesn't…
kjunz
  • 111
  • 1
  • 7
-4
votes
1 answer

Internal working of Java Collections Framework

I've had this question for a very very long time. Question is a bit long. Please bear with me :) In Summary, how does a collection datastructure such as TreeSet know when the underlying data it stores gets modified and how does it manage such…
Adithya Upadhya
  • 2,239
  • 20
  • 28
-4
votes
1 answer

insert elements in the collection based on multiple keys

I Have a query result with many fields, and i have to insert only unique rows in the collection based on the 3 columns of the table using java code. Suppose query result is like below : Col A | Col B | Col C | Col D| Col E 1 | 2 | 1 | V1 | A 1 | 2 |…
-4
votes
1 answer

how to prenvent an existed name to rewrite

i have a problem that i want when i enter a name of library in nameText (type Text ) and the name already existed in the list of libraries, the button ok is blocked und i cannot do that (a messsage displays and informs that i would write another…
ange
  • 1
  • 5
-5
votes
1 answer

Method remove() of TreeSet type return

So I was looking at the method remove() from the class TreeSet, and the method has e return type boolean. The java api says that if the item we want to remove is in the tree, and is removed than the method returns true. What if the item is not in…
FranXh
  • 4,481
  • 20
  • 59
  • 78
-6
votes
1 answer

java how compare function works

I am trying to break the rule of set by allowing the same object to be twice (just for fun) This is my code: class Person implements Comparable { public String firstName; public Person(String firstName) { this.firstName =…
Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253
1 2 3
41
42