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

Why doesn't TreeSet.contains() work?

public class Empty { public static void main( String[] args ) { TreeSet classes = new TreeSet(); classes.add( String.class ); String test = new String(); try{ if(…
oliholz
  • 7,447
  • 2
  • 43
  • 82
5
votes
2 answers

How is 1 greater than 4?

The NavigableSet.lower(E) Javadoc says it returns the greatest element in this set strictly less than the given element, or null if there is no such element. Why is 1 the output here? Shouldn't it be 4? NavigableSet original = new…
Leo
  • 5,017
  • 6
  • 32
  • 55
5
votes
2 answers

Conventions used in Java documentation

Why does the Oracle Java API documentation for the add() method for TreeSet and HashSet state that: an element e is added only if there is no e2 in the set where (e==null ? e2==null : e.equals(e2)) However, TreeSet uses compareTo(), while HashSet…
5
votes
1 answer

Alphabetical sorting in treeset not working

Hi, my code is like this: TreeSet ts=new TreeSet(); ts.add("Testtxt"); ts.add("Testxml"); ts.add("docdoc"); ts.add("ePeoplexml"); ts.add("fantasyxlsx"); ts.add("idaddedgif"); ts.add("idaddedrtf"); System.out.println("Tree set ::…
Madhu Sudhan Reddy
  • 607
  • 5
  • 15
  • 22
5
votes
2 answers

What is the Time Complexity of size() for Sets in Java?

I know, it seems like a stupid question, you would expect that the time complexity of size() on any collection would be O(1) - but I'm finding that an "optimization" in my code which requires a call to size() is actually slowing things down. So,…
Zarjio
  • 1,097
  • 2
  • 12
  • 22
5
votes
1 answer

How do addAll in TreeSet and HashSet really work?

When I added my datas on the treeSet I lost almost all my datas. It seems that I have only the first element of my set. I read this question Hashset vs Treeset for an code optimization and I tried to do something like them. But I didn't really…
M07
  • 1,060
  • 1
  • 14
  • 23
4
votes
1 answer

SortedSet for integers with GNU trove

I'm migrating some code over to GNU trove for performance reasons. However, I do have some TreeSets, where I need rather fast update and lookups along with a sorted iteration - the primary use case of a TreeSet. Of course I will go over the use and…
Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
4
votes
1 answer

TreeSet Custom Comparator Algo .. String Comparision

From the input string provided: { "200,400,7,1", "100,0,1,1", "200,200,3,1", "0,400,11,1", "407,308,5,1","100,600,9,1" } , I am adding the same in a TreeSet and want it to be sorted with the 3rd element order, so the expected output will…
Abhishek Choudhary
  • 8,255
  • 19
  • 69
  • 128
4
votes
2 answers

Searching for a record in a TreeSet on the fly

I'm writing a contact book application in Java using the swing and awt libraries. The Application consists of a JList which uses a TreeSet as an abstractListModel. The TreeSet is for a class called Contact, which has private comparator class that…
W.K.S
  • 9,787
  • 15
  • 75
  • 122
4
votes
1 answer

Android dictionary TreeSet faster load time

I have like 300000 words in my dictionary (actually saved in txt format (new line delimited) on sdcard of my Android device). I want to build data structure that would take as less time as possible to insert words (String-s) from my txt file in this…
zmeda
  • 2,909
  • 9
  • 36
  • 56
4
votes
3 answers

Use Constructor with Lambda expression (Set Comparator)

I am given the task to use the constructor TreeSet(Comparator comparator) with a lambda expression to make the set be sorted by book title. The following information is given: public class Book implements Comparable { private String…
JavaMan
  • 77
  • 1
  • 2
4
votes
1 answer

Removing an object from TreeSet

So I have the following relevant code: public static TreeSet set; public static void remove(Object object){ for (Object o : set){ if (o.equals(object)){ System.out.println("removing " + o); …
Lukas Tilmann
  • 73
  • 1
  • 5
4
votes
2 answers

Leetcode220 error: incomparable types: int and

I received an error when compiled the code below, which is "incomparable types: int and " on line 10. public class Solution { public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) { if (nums == null || k <= 0 || t < 0 ||…
OhhhIrisHere
  • 43
  • 1
  • 3
4
votes
2 answers

Java Object Ordering for Sorted Collections

When I'm looking at the Java Object Ordering tutorial, the last section 'Comparators' of the article confused me a little bit. By defining a class Employee which itself is comparable by employee's name, the tutorial doesn't show if this class has…
lkq
  • 2,326
  • 1
  • 12
  • 22
4
votes
1 answer

Java Comparator, Comparable and TreeSet.contains

edit - slightly simplified example below (any simpler and i'm not sure it contains all of the elements of the desired behavior) Below is a code snippet that represents one thing I'm trying to do with Comparator. I would like for contains to return…
mikesol
  • 1,177
  • 1
  • 11
  • 20