美文网首页
Collections.sort()(二)----理解泛型<?

Collections.sort()(二)----理解泛型<?

作者: 水土jun | 来源:发表于2017-09-23 17:46 被阅读0次

    下面一段话来自一位知乎用户的回答,特摘抄如下:

    假设现在有这么一个类的继承树,Plant -> Fruit -> Apple -> RedApple。
    List<? extends Fruit> appList2的意思,是一个列表,这个列表里面的元素是Fruit的某个子类T,那么在从appList2中取出一个元素时,编译器会自动把这个元素转型为T。那么现在假设T是RedApple,很显然你往这样一个appList2中add一个Apple类型的元素,取出后转型为RedApple必然会失败;同样的情况如果T是Apple,你add一个Fruit类型的元素,取出后转型为Apple,也会抛出异常。也就是说,编译器会把列表中取出的元素转型为某种类型,但编译器不确定这种转型是不是会成功,即在不保证运行时能顺利进行,因此就不允许你add任何类型的元素。
    再来看看List<? super Fruit> appList,这个列表的元素都是Fruit的父类T,也就是说当你从这个列表中get一个元素时,编译器会自动加一句转型为T的代码。好,现在你往appList中add一个Apple类型的元素,取出时转型为T,由于T是Apple的父类,向上转型没有问题;加一个RedApple类型的元素,也一样不会有问题。也就是说,只要保证你往appList中添加的元素是Fruit的子类,编译器就可以保证在转型为T时不会抛出异常。因此第二种写法可以过编译。

    还是用前面文章(http://www.jianshu.com/p/5633ab144569)学生排序的例子。
    学生类:

    package Compara;
    
    public class Student  {
        //定义学生类的名字name和分数mark属性
        public String name;
        public int mark;
        //定义构造方法
        public Student(String name, int mark) {
            super();
            this.name = name;
            this.mark = mark;
        }
    }
    

    自定义的比较器StudentComparator:

    package Compara;
    
    import java.util.Comparator;
    
    public class StudentComparator implements Comparator<Student> {
        
        @Override
        public int compare(Student o1, Student o2) {
            if (o1.name.compareTo(o2.name) > 0) {
                return 1;
            } else if (o1.name.compareTo(o2.name) == 0) {
                if (o1.mark - o2.mark > 0) {
                    return 1;
                } else if (o1.mark - o2.mark == 0) {
                    return 0;
                } else {
                    return -1;
                }
            } else {
                return -1;
            }
        }
    
    }
    

    再自定义一个女学生类,继承学生类:

    package Compara;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class GirlStudent extends Student{
        public String name;
        public int mark;
        public String sex="women";
    
        public GirlStudent(String name, int mark) {
            super(name, mark);
            
        }
    
        public static void main(String[] args) {
            //new出来四个学生对象
            GirlStudent girlstu1=new GirlStudent("Limei",568);
            GirlStudent girlstu2=new GirlStudent("Chenxiaojuan",512);
            GirlStudent girlstu3=new GirlStudent("Limei",620);
            GirlStudent girlstu4=new GirlStudent("Zhouli",522);
            //new一个list,将四个学生扔进去。
            List<Student> girlstudentlist=new ArrayList<Student>();
            girlstudentlist.add(girlstu1);
            girlstudentlist.add(girlstu2);
            girlstudentlist.add(girlstu3);
            girlstudentlist.add(girlstu4);
            //条用Collections下的sort方法进行排序。
            Collections.sort(girlstudentlist,new StudentComparator());
            //打印出排序后的情况
            for(Student stu:girlstudentlist){
                System.out.println(stu.name+"=>"+stu.mark);
            }
        }
            
    
    }
    

    结果:

    Chenxiaojuan=>512
    Limei=>568
    Limei=>620
    Zhouli=>522
    

    我们看源码Collecctions中sort方法:

     public static <T> void sort(List<T> list, Comparator<? super T> c) {
            list.sort(c);
        }
    

    Comparator<? super T> 表示是T的父类?的比较器。这里的?在例子中指的是Student类,它是GirlStudent类的父类。

    相关文章

      网友评论

          本文标题:Collections.sort()(二)----理解泛型<?

          本文链接:https://www.haomeiwen.com/subject/jkicextx.html