A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 李杰 初级黑马   /  2012-9-2 09:58  /  2018 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

具体的跟我讲一下,顺便举几个经典的例子,
告诉我黑马教学视频在哪讲这个了
谢谢!!!

1 个回复

倒序浏览
//在毕老师的基础视频的第15天中有。
import java.util.*;

/*
当元素自身不具备比较性,或者具备的比较性不是所需要的。
这时需要让容器自身具备比较性。
定义了比较器,将比较器对象作为参数传递给TreeSet集合的构造函数。

当两种排序都存在时,以比较器为主。

定义一个类,实现Comparator接口,覆盖compare方法。


*/
class Student implements Comparable//该接口强制让学生具备比较性。
{
        private String name;
        private int age;

        Student(String name,int age)
        {
                this.name = name;
                this.age = age;
        }

        public int compareTo(Object obj)
        {

                //return 0;
               
                if(!(obj instanceof Student))
                        throw new RuntimeException("不是学生对象");
                Student s = (Student)obj;

                //System.out.println(this.name+"....compareto....."+s.name);
                if(this.age>s.age)
                        return 1;
                if(this.age==s.age)
                {
                        return this.name.compareTo(s.name);
                }
                return -1;
                /**/
        }

        public String getName()
        {
                return name;

        }
        public int getAge()
        {
                return age;
        }
}
class TreeSetDemo2
{
        public static void main(String[] args)
        {
                TreeSet ts = new TreeSet();//此处可以加入比较器,TreeSet ts = new TreeSet(new MyCompare ()); 排序就会以比较器的方法排。                ts.add(new Student("lisi02",22));
                ts.add(new Student("lisi02",21));
                ts.add(new Student("lisi007",20));
                ts.add(new Student("lisi09",19));
                ts.add(new Student("lisi06",18));
                ts.add(new Student("lisi06",18));
                ts.add(new Student("lisi007",29));
                //ts.add(new Student("lisi007",20));
                //ts.add(new Student("lisi01",40));

                Iterator it = ts.iterator();
                while(it.hasNext())
                {
                        Student stu = (Student)it.next();
                        System.out.println(stu.getName()+"..."+stu.getAge());
                }
        }
}

class MyCompare implements Comparator
{
        public int compare(Object o1,Object o2)
        {
                Student s1 = (Student)o1;
                Student s2 = (Student)o2;

                int num = s1.getName().compareTo(s2.getName());
                if(num==0)
                {

                        return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
                        /*
                        if(s1.getAge()>s2.getAge())
                                return 1;
                        if(s1.getAge()==s2.getAge())
                                return 0;
                        return -1;
                        */
                }

               
                return num;

        }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马