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
11
votes
3 answers

TreeSet and equals function

There is a Java bean object which has implemented equals function based on certain criteria (Criteria A). I have a requirement to identify unique objects based on another criteria (Criteria B). Since the equals function uses criteria A, I can not…
Sujee
  • 4,985
  • 6
  • 31
  • 37
11
votes
2 answers

Complexity of treeset's subset method?

I am looking for time complexity of subset method of treeset. Is it O(n) where n is the number of items in the navigation set ?
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
11
votes
7 answers

java: TreeSet order

With this code I get this output: TreeSet t=new TreeSet(); t.add("test 15"); t.add("dfd 2"); t.add("ersfd 20"); t.add("asdt 10"); Iterator it=t.iterator(); while(it.hasNext()){ System.out.println(it.next); …
Enzo
  • 597
  • 1
  • 8
  • 22
11
votes
1 answer

Why does my TreeSet not add anything beyond the first element?

I have several arrays in the form: private static String[] patientNames = { "John Lennon", "Paul McCartney", "George Harrison", "Ringo Starr" }; Then I make a TreeSet like this: TreeSet patTreeSet = new TreeSet(); Where Patient…
Jona
  • 1,023
  • 2
  • 15
  • 39
11
votes
1 answer

What is complexity of size() for TreeSet portion view in Java

I'm wondering what is the time complexity of size() for portion view of TreeSet. Let say I'm adding random numbers to set (and I do not care about duplicities): final TreeSet tree = new TreeSet(); final Random r = new…
Betlista
  • 10,327
  • 13
  • 69
  • 110
11
votes
1 answer

Equals and Comparable with Sets

I posted some code here which correctly solved a problem the poster had. OP wanted to remove duplicates and bring certain special items to the top of a list. I used a TreeSet with a special Comparable class which wrapped the Locale they were working…
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
11
votes
4 answers

TreeSet internally uses TreeMap, so is it required to implement Hashcode method when using Treeset?

I would like to know what it means when javadocs for TreeSet says This class implements the Set interface, backed by a TreeMap instance? In the below example, I haven't implemented the Hashcode method and still it is working as per expectation…
Metalhead
  • 1,429
  • 3
  • 15
  • 34
10
votes
3 answers

Comparator suitable for TreeSet when there is no distinguishing field

Suppose I have a class not implementing the Comparable interface like class Dummy { } and a collection of instances of this class plus some function external to the class that allows comparing these instances partially (a map will be used for this…
John McClane
  • 3,498
  • 3
  • 12
  • 33
10
votes
2 answers

Accessing next element in Treeset in Java

Is there any way to access the successor or the predecessor of an element in Treeset of Java. Or is there any way of getting the iterator of a particular element so that we can use iterator.next() to get the next element.
kaushalpranav
  • 1,725
  • 24
  • 39
10
votes
6 answers

How to reverse the order of SortedSet

I want to print an ordered list in Map using the following: Map mylist = new HashMap<>(); mylist.put(10.5, a); mylist.put(12.3, b); mylist.put(5.1, c); SortedSet orderlist = new TreeSet(mylist.keySet()); for (Float i…
user2109581
  • 187
  • 2
  • 2
  • 11
10
votes
4 answers

Computational complexity of TreeSet operations in Java?

I am trying to clear up some things regarding complexity in some of the operations of TreeSet. On the javadoc it says: "This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains)." So far so…
Andreas K.
  • 165
  • 1
  • 2
  • 7
10
votes
4 answers

Why does Java's TreeSet not specify that its type parameter must extend Comparable?

e.g. The code below throws a ClassCastException when the second Object is added to the TreeSet. Couldn't TreeSet have been written so that the type parameter can only be a Comparable type? i.e. TreeSet would not compile because Object is not…
Tarski
  • 5,360
  • 4
  • 38
  • 47
9
votes
5 answers

Remove duplicate objects from a ArrayList in Android

I know this has be discussed over and over again here, but none of the examples I've tried worked for me. What I've got I access the Call log from Android and I get a list of all calls made. Of course, here I get a lot of duplicates. First I make a…
Alin
  • 14,809
  • 40
  • 129
  • 218
9
votes
3 answers

Is there a TreeSet data structure equivalent in C++ with similar functions

I need to use a Tree Set data structure(available in java) in C++, and make use of functions like TreeSet.lower(i) and TreeSet.higher(i) - > which returns the element just lower, and just higher than i in the given tree set. Is there an STL? Edit:…
Batwoman05
  • 215
  • 1
  • 2
  • 9
9
votes
2 answers

Why is TreeSet Iteration O(n) instead of O(n*logn)?

I read a previous question about the time complexity for TreeSet and the answer was that it takes O(n) time. However, I don't understand why it is O(n) to iterate instead of O(n*nlogn). Each next call takes O(logn) time So if I iterate through a…
Eric
  • 2,008
  • 3
  • 18
  • 31
1 2
3
41 42