import java.util.*;
public class aa {
public static void main(String[] args) {
SortedSet set=new TreeSet(new Comparator()
{
public int compare(Object o1, Object o2) {
if(Integer.parseInt(o1.toString())>Integer.parseInt(o2.toString()))
return 1;
else
return
-1;
}
});
set.add("1");
set.add("5");
set.add("3");
System.out.println(set);
set.remove("1");//为什么删除不掉呢,请给详细解答!
System.out.println(set);
}
}
因为你重写了compare方法,所以你判断是否相等用的就是你自己的方法,但是你并没有给出如何才是相等,所以应该加上一个相等时返回0
如下:
[code="java"]
public int compare(Object o1, Object o2) {
if (Integer.parseInt(o1.toString()) > Integer.parseInt(o2
.toString()))
return 1;
else if (Integer.parseInt(o1.toString()) < Integer.parseInt(o2
.toString())) {
return -1;
} else {
return 0;
}
}
[/code]