zoukankan      html  css  js  c++  java
  • TreeSet中添加的数据,要求是相同类的对象

    1.向TreeSet中添加的数据,要求是相同类的对象。
    2.两种排序方式:自然排序(实现Comparable接口) 和 定制排序(Comparator)
    自然排序中,比较两个对象是否相同的标准为:compareTo()返回0.不再是equals().
    定制排序中,比较两个对象是否相同的标准为:compare()返回0.不再是equals().

    @Test
        public void test1() {
            TreeSet set = new TreeSet();
    
            //失败:不能添加不同类的对象
            //        set.add(123);
            //        set.add(456);
            //        set.add("AA");
            //        set.add(new User("Tom",12));
    
            //举例一:
            //        set.add(34);
            //        set.add(-34);
            //        set.add(43);
            //        set.add(11);
            //        set.add(8);
    
            //举例二:
            set.add(new User("Tom", 12));
            set.add(new User("Jerry", 32));
            set.add(new User("Jim", 2));
            set.add(new User("Mike", 65));
            set.add(new User("Jack", 33));
            set.add(new User("Jack", 56));
    
    
            Iterator iterator = set.iterator();
            while (iterator.hasNext()) {
                System.out.println(iterator.next());
            }
    
        }
    
        @Test
        public void test2() {
            Comparator com = new Comparator() {
                @Override
                public int compare(Object o1, Object o2) {
                    if (o1 instanceof User && o2 instanceof User) {
                        User u1 = (User) o1;
                        User u2 = (User) o2;
                         return u1.getAge()-u2.getAge();
                    }
                    return 0;
                }
                //按照年龄从小到大排列
                // @Override
                // public int compare(Object o1, Object o2) {
                //     if(o1 instanceof User && o2 instanceof User){
                //         User u1 = (User)o1;
                //         User u2 = (User)o2;
                //         return Integer.compare(u1.getAge(),u2.getAge());
                //     }else{
                //         throw new RuntimeException("输入的数据类型不匹配");
                //     }
                // }
            };
    
            TreeSet set = new TreeSet(com);
            set.add(new User("Tom", 12));
            set.add(new User("Jerry", 32));
            set.add(new User("Jim", 2));
            set.add(new User("Mike", 65));
            set.add(new User("Mary", 33));
            set.add(new User("Jack", 33));
            set.add(new User("Jack", 56));
    
    
            Iterator iterator = set.iterator();
            while (iterator.hasNext()) {
                System.out.println(iterator.next());
            }
        }
    
    会当凌绝顶,一览众山小
  • 相关阅读:
    MVC3、如何应用EntityFramework 连接MySql 数据库 Kevin
    DEV EXPRESS Summary Footer 不显示 Kevin
    装饰模式 Kevin
    Dev 控件 GridControl 控件 二次绑定数据源的问题。 Kevin
    System.InvalidOperationException 异常 Kevin
    LINQ to XML Kevin
    代理模式——代码版“吊丝的故事” Kevin
    VS2012 中的设备 面板 Kevin
    maven 学习笔记(三)创建一个较复杂的 eclipse+android+maven 工程
    maven 学习笔记(一)eclipse+android+maven
  • 原文地址:https://www.cnblogs.com/leyzzz/p/15333457.html
Copyright © 2011-2022 走看看