# Thread: Having trouble removing from TreeSet

1. Member
Join Date
Jan 2009
Posts
9
Rep Power
0

## Having trouble removing from TreeSet

Take a look. The remove operation never returns TRUE. What am I doing wrong?

Java 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);
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;
}```

2. Member
Join Date
Jan 2009
Posts
9
Rep Power
0
Disregard this I messed with the comparator in a weird way and it affected the remove method. Curious why if anybody knows.

3. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14
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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•