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
9
votes
5 answers

Understanding TreeSet when compareto returns 0

I have created a Student class like this: public class Student implements Comparable { private String firstName; private String lastName; public Student(String firstName, String lastName) { this.firstName = firstName; …
learner
  • 6,062
  • 14
  • 79
  • 139
9
votes
4 answers

Can't I put a null in a SortedSet?

I thought that null is allowed for a Set. So why does the following code: SortedSet set = new TreeSet(); set.add(null); set.add(1); //--->Line indicated by exception Gives the following exception? Exception in thread…
Cratylus
  • 52,998
  • 69
  • 209
  • 339
8
votes
4 answers

Why does Java's TreeSet remove(Object) not take an E

From the Java 6 TreeSet Documentation: boolean remove(Object o): Removes the specified element from this set if it is present. Why does this accept an Object instead of the generic type E? The only objects that can be added are of type E, so…
ComputerDruid
  • 16,897
  • 1
  • 19
  • 28
8
votes
5 answers

Returning an element from a TreeSet using binary search

In TreeSet there is a method called contains that returns true if an element is in the set. I assume that this method uses binary search and does not iterate through all the elements in ascending order. Am I right? I have a TreeSet that contains…
exent
  • 83
  • 1
  • 2
  • 4
8
votes
5 answers

What is the time complexity of ordered operations in TreeSet?

What is the time complexity of the following operations in java.util.TreeSet? first() last() lower() higher() I would assume that these are constant time but the API makes no guarantees.
signalseeker
  • 4,100
  • 7
  • 30
  • 36
8
votes
2 answers

Should I use a `HashSet` or a `TreeSet` for a very large dataset?

I have a requirement to store 2 to 15 million Accounts (which are a String of length 15) in a data structure for lookup purpose and checking uniqueness. Initially I planned to store them in a HashSet, but I doubt the speed of the lookup will be slow…
Mohan
  • 282
  • 2
  • 16
8
votes
2 answers

What is the time complexity of TreeSet iteration?

In my code, Java TreeSet iteration is the dominant time factor. In looking at the system I believe that it is O(n) complexity. Can anyone verify this? I am thinking that by providing links backward from child node to parent node I could improve the…
John Smith
  • 81
  • 1
  • 3
8
votes
2 answers

Scala's TreeSet vs Java's TreeSet - poll?

If I want to remove the highest entry in log(n) time in Java's TreeSet, I use treeSet.pollFirst() - what is the equivalent for Scala's mutable.TreeSet class? Anyway, what I really want is a heap-like priority queue data structure that lets me…
pathikrit
  • 32,469
  • 37
  • 142
  • 221
7
votes
2 answers

Java - Most efficient way to convert a TreeSet into a String[]?

I was doing this: (String[]) myTreeSet.toArray(); but that gives me a ClassCastException at runtime. The only thing I can think of doing is just making an array first and then iterating through each element in myTreeSet and adding it to the array.…
Tim
  • 4,295
  • 9
  • 37
  • 49
7
votes
4 answers

How can I use a custom class in a TreeSet?

If I was using a Set similar to this: Set s=new TreeSet(); class node { private int x; private int y; } Would this be acceptable, and since it's a TreeSet, would it also sort it?
SNpn
  • 2,157
  • 8
  • 36
  • 53
7
votes
2 answers

Java TreeSet with length Comparator bug?

I have the below code which creates a TreeSet using a comparator based on string length. public class TreeSetComparator { public static void main(String[] args) { SortedSet sortedSet = new…
Georgios F.
  • 417
  • 5
  • 15
7
votes
2 answers

Java - TreeSet accepting duplicates

I'm having some problems with TreeSet: why does this one accept duplicates? I thought TreeSets detected them through the Comparator, and automatically remove them. Please help me, I'm kind of new to both Java and StackOverflow. import…
M. P.
  • 161
  • 1
  • 6
7
votes
3 answers

How do I sort a List of TreeSets with java8 streams

My list contains sets like [1,3,5][2,6,4] etc, all of the same size. I tried doing this but it doesn't seem to work. List> block; for(TreeSet t : block){ …
LexByte
  • 382
  • 4
  • 15
7
votes
2 answers

use of hashCode() and equals() in TreeSet and TreeMap

From the following code, I understand that, there is no need of overriding equals() and hashCode() method for TreeSet and TreeMap, neither for sorting, nor searching. public class ComparableTest implements Comparable { private…
Anushree Acharjee
  • 854
  • 5
  • 15
  • 30
7
votes
2 answers

Why scala's TreeSet returns SortedSet

Is there a reason that the object TreeSet.apply method returns SortedSet and not TreeSet? The following code won't compile in scala 2.7 val t:TreeSet[Int] = TreeSet(1,2,3)
Elazar Leibovich
  • 32,750
  • 33
  • 122
  • 169