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

Why is TreeSet declared TreeSet instead of TreeSet>

I was working with TreeSet and found a ClassCastException while calling TreeSet#add() method. Code: public class Testing { public static void main(String[] args) { TreeSet ts = new TreeSet<>(); ts.add(new Testing()); …
Codebender
  • 14,221
  • 7
  • 48
  • 85
3
votes
1 answer

Synced TreeSet Cast?

I am using a TreeSet in Java to hold a list of integers: TreeSet num = new TreeSet(); Since my application has multiple threads, I want to be able to access it synchronously. I'm using Collections.synchronizedSortedSet(num); and…
Sameer Puri
  • 987
  • 8
  • 20
3
votes
4 answers

Difference between these declaration of TreeSet

So I have to use a TreeSet in my code. As TreeSet extends AbstractSet implements NavigableSet, Cloneable, java.io.Serializable and interface NavigableSet extends SortedSet which extends Set I can use any of these three…
Zeeshan
  • 11,851
  • 21
  • 73
  • 98
3
votes
2 answers

Java TreeSet: duplicate elements?

I want to have some pairs of int in a TreeSet, and sort them by the first number. Code test here: public static void main (String[] args) throws java.lang.Exception { SortedSet s = new TreeSet(new Comparator(){ …
Meng Wang
  • 277
  • 1
  • 8
3
votes
1 answer

SortedSet::removeAll( headSet ) fails, but deriving another collection from headSet succeeds. Why?

From a TreeSet (a SortedSet) in Java 8: I call the ::headSet method to get a SortedSet of the objects at the front of the sorted collection. I call ::removeAll to delete those frontmost objects.BAM, throws a ConcurrentModificationException. Yet…
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
3
votes
0 answers

Is it necessary to override equals and hashCode methods in a class if I use the objects of class to insert in a TreeSet only?

Suppose I have below class definition: public class Student implements Comparable { private int id; private String name; @Override public int compareTo(Student o) { return this.id - o.id; } } Can I use it for…
hsingh
  • 661
  • 5
  • 26
3
votes
3 answers

TreeSet not adding all elements?

I have been looking into the speeds of different Java collection types and have come across something weird. I am adding 1,000,000 objects from a static array to a different collection type and returning the time required. This part of the code…
Jacob Hobbs
  • 83
  • 1
  • 3
3
votes
3 answers

Datastructure ordered by insertion which can get length of subset

I am looking for a datastructure in Java sorted by insertion which can quickly find and remove a specific element and count the number of elements added after this element. LinkedHashSet theoretically fulfills this requirement, but the interface…
trevore
  • 425
  • 2
  • 10
3
votes
4 answers

Find the highest N numbers in an infinite list

I was asked this question in a recent Java interview. Given a List containing millions of items, maintain a list of the highest n items. Sorting the list in descending order then taking the first n items is definitely not efficient due to the list…
gsdev
  • 259
  • 4
  • 14
3
votes
3 answers

How does the ceiling , floor will behave on TreeSet of String

I am reading about the TreeSet and found it interesting. I have one question lets we have a TreeSet of String. How does this Ceiling and floor function will behave in this case. NavigableSet ns= new TreeSet(); …
dead programmer
  • 4,223
  • 9
  • 46
  • 77
3
votes
1 answer

Sorting a hashmap

I need to sort a hash map according to the key.The key is a string(so I need to sort it alphabetically) and the value is an integer. I was trying to search online and found that tree set automatically sorts it once you put it. Could somebody guide…
bawa
  • 93
  • 1
  • 1
  • 7
3
votes
2 answers

How to have a TreeSet which is "inconsistent with equals"

I have read numerous posts about TreeSets, Comparable/Comparator Interfaces, equals, compareTo, compare methods and I know that API says you have to make your ordering "consistent with equals" or weird things might happen. But in my case and I think…
akifusenet
  • 192
  • 6
3
votes
3 answers

VB.NET equivalent to java.util.TreeSet

Is there a VB.NET equivalent to java.util.TreeSet?
Roger
  • 31
  • 1
  • 2
3
votes
1 answer

Finding entries in a TreeSet that contain a prefix

I have a TreeSet in Java that contains Strings (specifically words). I need to write a method... public boolean isValidPrefix(String prefix) ...which accepts a prefix as an argument and checks the TreeSet to see if any of its contained words begin…
ordanj
  • 361
  • 1
  • 8
  • 19
3
votes
4 answers

How to find the next in order successor in a binary tree?

I'm trying to implement an Iterator in my own TreeSet class. However my attempt at creating it only works until the current node is the root. The Iterator looks like this: Constructor: public TreeWordSetIterator() { next = root; if(next ==…
user3368214
  • 33
  • 1
  • 4