treeset

Treeset to order elements in descending order

Here is the piece of code that I have used for Java 5.0 TreeSet<Integer> treeSetObj = new TreeSet<Integer>( Collections.reverseOrder() ) ; Collections.reverseOrder() is used to obtain a comparator in order to reverse the way the elements are stored and iterated. Is there a more optimized way of doing it? ...

[Java] What are the pros and cons of a TreeSet

Just wondering what the pros and cons of a TreeSet is, if anyone could tell me please? Thanks! ...

Hashset vs Treeset

I've always loved trees, that nice O(n*lg(n)) and the tidyness of them. However, every software engineer I've ever known has asked me pointedly why I would use a treeset. From a CS background, I don't think it matters all that much which you use, and I don't care to mess around with hash functions and buckets (in the case of Java). In w...

Java - TreeSet and hashCode()

I have a quick question about TreeSets and hashCodes. I have a TreeSet and I'm adding objects to it, before I add an object, I check to see if it exists in the TreeSet using the contains method. I have 2 distinct objects, each of which produce a distinct hashCode using my implementation of the hashCode method, example below: public int...

copying a bst to a treeSet

I have to write a method that returns a set of all elements in a bag with no duplicate entries allowed. The data is held in a BST, and I'm trying to do this by using a TreeSet, but I'm having trouble working out how to traverse the tree and add each node's data to the TreeSet. I can only manage the following: Set<E> uniqueSet() { No...

Java: Problems with TreeSet

I have a class Odp. I want to use TreeSet to keep a sorted collection of Odp objects. However, I've been having problems. public class OdpStorage { private TreeSet<Odp> collection = new TreeSet<Odp>(); public addOdp(Odp o) { return collection.add(o); } public int size() { return collection.size();...

Java: Trouble with TreeSet and LinkedList

I have an unsorted linked list. To sort it, I thought I'd put the values into a TreeSet with a comparator supplied, then return those values as a new linked list. Yet, it fails. Comparator: public class SortSpeciesByCommonName implements Comparator<Species> { /** * a negative integer, zero, or a positive integer as the first ...

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) ...

VB.NET equivalent to java.util.TreeSet

Is there a VB.NET equivalent to java.util.TreeSet? ...

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 Comparable. That way generics actually do their job - of being typesafe. import java.util.TreeSe...

Java's TreeSet equivalent in Python?

I recently came across some Java code that simply put some strings into a Java TreeSet, implemented a distance based comparator for it, and then made its merry way into the sunset to compute a given score to solve the given problem. My questions, Is there an equivalent data structure available for Python? The Java treeset looks basi...

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 performance. ...

How to add to SortedSet items from an Array?

I have a SortedSet defined this way: SortedSet<RatedMessage> messageCollection = new TreeSet<RatedMessage>(new Comp()); and I have an array of RatedMessage[] I had to use the array as the set misses the serialization feature, now I need to construct it back. Is there a quick way to add all the items from the array to the set again?...

Computational complexity of TreeSet operations in Java?

Hello there. 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 good. My question is what happens on addAll(), removeAll() etc. Here t...

Treeset.contains() problem.

So I've been struggling with a problem for a while now, figured I might as well ask for help here. I'm adding Ticket objects to a TreeSet, Ticket implements Comparable and has overridden equals(), hashCode() and CompareTo() methods. I need to check if an object is already in the TreeSet using contains(). Now after adding 2 elements to t...

Why doesn't TreeSet.contains() work?

public class Empty { public static void main( String[] args ) { TreeSet<Class> classes = new TreeSet<Class>(); classes.add( String.class ); String test = new String(); try{ if( classes.contains(test.getClass()) ){ System.out.println( "contains" ); } ...

At what level is a given element in a Java TreeSet?

Does anybody know a fast way to detect at what level a given element is in a TreeSet? By level, I mean the depth of this element in the tree, i.e. the number of its ancestors. Background. I use Java's TreeSet class to store my elements. To compare two elements I need to compute some auxiliary information about them. I cannot store this ...

TreeSet and equals function

Hi, 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 use HashSet. So I thought of using TreeSet with my custom Comparator which is based on ...

Java Point2D and Comparable

I'm making a TreeSet of Point2D.Float type, creating it with a custom Comparable class that implements compare() of Point2D type. However, on calling "contains" on the TreeSet, I get a classcast error: java.lang.ClassCastException: java.awt.geom.Point2D$Float cannot be cast to java.lang.Comparable The set is created as so: private Coor...

When do you know when to use a TreeSet or LinkedList?

What are the advantages of each structure? In my program I will be performing these steps and I was wondering which data structure above I should be using: Taking in an unsorted array and adding them to a sorted structure1. Traversing through sorted data and removing the right one Adding data (never removing) and returning that struct...