compare方法是在Comparator接口里定义的:public int compare(Object o1,Object o2)Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

解决方案 »

  1.   

    如果 a < b 返回 负数
    a == b 返回 0
    a > b返回 正数
    这样在比较的类中就可以调用外部的比较函数确定位置TreeSet的参数应该作用是在你每次插入add("d");时找到一个合适的位置使得它们可以
    之后的顺序保持正确
      

  2.   

    MyComp中将原有的比较关系取反,得到的结果当然是逆序的。
    TreeSet ts=new TreeSet(new MyComp());传入的参数new MyComp()是排序是得比较函数。
      

  3.   

    在TreeSet的构造函数里面
    有public TreeSet(Comparator c)
    但是说明中提到Constructs a new, empty set, sorted according to the specified comparator. All elements inserted into the set must be mutually comparable by the specified comparator
    参看原贴中应用了Comparator接口的类MyCom
    它的方法compare返回的int怎么能使TreeSet的构造函数Constructs a new, empty set, sorted according to the specified comparator?
    我就是搞不懂这一点