import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class test{
/**
 * @param args
 * @param stud)en 
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
Set<student> treeset = new TreeSet(new Comparator(){
@Override
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub
if (((student)o1).age>((student)o2).age) return -1;
System.out.println(((student)o1).name+"  "+((student)o2).name);
        if (((student)o1).age<((student)o2).age) return 1;
        if (((student)o1).age == ((student)o2).age){
         return ((student)o1).name.compareTo(((student)o2).name);
        }
return 0;
});
treeset.add(new student("wanglin",19));
treeset.add(new student("wangyanlei",27));
treeset.add(new student("shuia",29));
treeset.add(new student("hello",29));
System.out.println(treeset.size());
Iterator<student> iter = treeset.iterator();
while (iter.hasNext()){
System.out.println(iter.next().name.toString());
}
}}
在构造比较器的时候不知道哪有错,总是自身比较,在这个程序基础上应该怎么改呢?

解决方案 »

  1.   

    稍微改了一下,运行没有任何问题:package com.java.test.comparator;import java.util.Comparator;
    import java.util.Iterator;
    import java.util.Set;
    import java.util.TreeSet;public class TreeSetTest { @SuppressWarnings("unchecked")
    public static void main(String[] args) {
    Set<Student> treeset = new TreeSet(new Comparator() {
    public int compare(Object o1, Object o2) {
    if (((Student) o1).age > ((Student) o2).age)
    return -1;
    if (((Student) o1).age < ((Student) o2).age)
    return 1;
    if (((Student) o1).age == ((Student) o2).age) {
    return ((Student) o1).name.compareTo(((Student) o2).name);
    }
    return 0;
    }
    }); treeset.add(new Student("wanglin", 19));
    treeset.add(new Student("wangyanlei", 27));
    treeset.add(new Student("shuia", 29));
    treeset.add(new Student("hello", 29));
    System.out.println(treeset.size());
    Iterator<Student> iter = treeset.iterator();
    while (iter.hasNext()) {
    System.out.println(iter.next().name.toString());
    } }}
      

  2.   

    楼主的student类有问题。代码贴出来。可能是equal或hashcode方法有问题。
      

  3.   

    这个是运行的结果,不知道到底哪错了
    Student Age的比较:
    wangyanlei   wangyanlei
    shuia   shuia
    hello   hello
    Treeset的Size
    1
    TreeSet:
    hello
      

  4.   

    每次add进去的时候,TreeSet只有1个节点数据。
      

  5.   

    为啥 我这里报错啊
    if (((Student) o1).age > ((Student) o2).age)
      

  6.   

    package sonst;import java.util.Comparator;
    import java.util.Iterator;
    import java.util.Set;
    import java.util.TreeSet;public class TreeSetTest { public static void main(String[] args) {
    Set<student> treeset = new TreeSet<student>(new Comparator<student>() { public int compare(student o1, student o2) {
    if (o1.age > o2.age)
    return -1;
    System.out.println(o1.name + "  " + o2.name);
    if (o1.age < o2.age)
    return 1;
    if (o1.age == o2.age) {
    return o1.name.compareTo(o2.name);
    }
    return 0;
    }
    });
    treeset.add(new student("wanglin", 19));
    treeset.add(new student("wangyanlei", 27));
    treeset.add(new student("shuia", 29));
    treeset.add(new student("hello", 29)); System.out.println(treeset.size());

    Iterator<student> iter = treeset.iterator();
    while (iter.hasNext()) {
    System.out.println(iter.next().name.toString()); } }
    }class student {
    public String name;
    public int age; public student(String aName, int aAge) {
    name = aName;
    age = aAge;
    }
    }
    运行结构:hello  shuia
    4
    hello
    shuia
    wangyanlei
    wanglin