# Having trouble removing from TreeSet

• 05-02-2009, 02:46 AM
xtrmi
Having trouble removing from TreeSet
Take a look. The remove operation never returns TRUE. What am I doing wrong?

Code:

```public static double closestPoints (Point [] xQueue) {                         Point closestLeft, closestRight, current, check,tmp;             int c,n,t;             double delta, newDelta;             yComparator yC = new yComparator();         // initX **************************             Arrays.sort(xQueue); // sort xQueue by X-coordinate                   // initY  **************************             TreeSet<Point> yTree = new TreeSet<Point>(yC); // Creates empty tree set                         closestLeft = xQueue[0];             closestRight = xQueue[1];             delta = distance(closestLeft, closestRight);             yTree.add(xQueue[0]);             yTree.add(xQueue[1]);             c = 2;             t = 0;             n = xQueue.length;                         while ( c < n ) {                                 current = xQueue[c];                 while ( t < c && (double)(current.x - xQueue[t].x) >= delta) {                     if ( yTree.remove(xQueue[t]))                     System.out.println("REMOVED ----> " + xQueue[t].x + ":" + xQueue[t].y);                     t = t + 1;                 }```
• 05-02-2009, 03:13 AM
xtrmi
Disregard this I messed with the comparator in a weird way and it affected the remove method. Curious why if anybody knows.
• 05-02-2009, 04:01 AM
pbrockway2
I would expect yTree.remove() to return false becuase, as the comment in your code notes, you create an empty tree. You add the end points from the array but no other points.

Even if you "add" the other points whether they are actually added (and therefore whether remove() will return true) is going to depend on how you implement the comparator. See my reply in your other thread.