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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黑马-王燚 中级黑马   /  2012-10-3 01:42  /  1659 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.util.*;

public class GenericDemo3 {

        /**
         * @param args
         */
        public static void main(String[] args)
        {
                // TODO 自动生成的方法存根
                TreeSet<Student_Human> t=new TreeSet<Student_Human>(    /*new Comp()*/    );
                t.add(new Student_Human("abc01"));
                t.add(new Student_Human("abc02"));
                t.add(new Student_Human("abc07"));
                t.add(new Student_Human("abc03"));
                t.add(new Student_Human("abc04"));
               
                Iterator<Student_Human> it=t.iterator();
                while(it.hasNext())
                {
                        System.out.println(it.next().getName());
                }
        }

}
class Comp implements Comparator<Student_Human>
{

        @Override
        public int compare(Student_Human s1, Student_Human s2)
        {
               
                return s1.getName().compareTo(s2.getName()); //升序排序(按ASCII码从低到高)
        }
       
}


我想问的是 为什么这里把new Comp()比较器去掉 就会报错呢,报的错误是

Exception in thread "main" java.lang.ClassCastException: com.CollectionDemo.Student_Human cannot be cast to java.lang.Comparable

评分

参与人数 1技术分 +1 收起 理由
唐志兵 + 1 赞一个!

查看全部评分

2 个回复

倒序浏览
本帖最后由 黄小贝 于 2012-10-3 02:29 编辑

目测你的Student_Human没有实现Comparable接口,TreeSet的add方法调用了TreeMap的put方法

如果比较器为null ,则插入的元素需要实现Comparable 接口,所以~你要么传一个比较器进去,要么实现Comparable  接口~
  1. public V put(K key, V value) {
  2.         Entry<K,V> t = root;
  3.         if (t == null) {
  4.             // TBD:
  5.             // 5045147: (coll) Adding null to an empty TreeSet should
  6.             // throw NullPointerException
  7.             //
  8.             // compare(key, key); // type check
  9.             root = new Entry<K,V>(key, value, null);
  10.             size = 1;
  11.             modCount++;
  12.             return null;
  13.         }
  14.         int cmp;
  15.         Entry<K,V> parent;
  16.         // split comparator and comparable paths
  17.         Comparator<? super K> cpr = comparator;
  18.         if (cpr != null) {
  19.             do {
  20.                 parent = t;
  21.                 cmp = cpr.compare(key, t.key);
  22.                 if (cmp < 0)
  23.                     t = t.left;
  24.                 else if (cmp > 0)
  25.                     t = t.right;
  26.                 else
  27.                     return t.setValue(value);
  28.             } while (t != null);
  29.         }
  30.         else {
  31.             if (key == null)
  32.                 throw new NullPointerException();
  33.             Comparable<? super K> k = (Comparable<? super K>) key;
  34.             do {
  35.                 parent = t;
  36.                 cmp = k.compareTo(t.key);
  37.                 if (cmp < 0)
  38.                     t = t.left;
  39.                 else if (cmp > 0)
  40.                     t = t.right;
  41.                 else
  42.                     return t.setValue(value);
  43.             } while (t != null);
  44.         }
  45.         Entry<K,V> e = new Entry<K,V>(key, value, parent);
  46.         if (cmp < 0)
  47.             parent.left = e;
  48.         else
  49.             parent.right = e;
  50.         fixAfterInsertion(e);
  51.         size++;
  52.         modCount++;
  53.         return null;
  54.     }
复制代码

评分

参与人数 1技术分 +1 收起 理由
唐志兵 + 1 赞一个!

查看全部评分

回复 使用道具 举报
TreeSet集合会对存入的元素排序,要求存入的元素本身具备比较性,这时候存入的元素要么本身实现了Comprable

接口Comprable强行对实现它的每个类的对象进行整体排序。这种排序被称为类的自然排序
复写接口中的compareTO()方法,按照指定方式进行排序,

如果元素不具备比较性,就是说没有实现Comparable接口,这时候需要让集合容器自身具备比较性
当两种排序都存在的时候,以比较器Comparator为主


你上面的自定义对象类并没有实现Comparable接口,不具备比较性
如果这时候不传入比较器,肯定是不可以的
存入一个元素没问题,接着存入的时候编译没有问题,但是运行的时候出异常,ClassCastException,类型转换异常。


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