码迷,mamicode.com
首页 > 其他好文 > 详细

泛型限定的练习。获取Collection集合中的最大值

时间:2016-05-03 21:56:15      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

/**
 *    泛型限定的练习。★★★★★
            获取Collection集合中的最大值。(解决问题按照一下的要求做)
      1 先定义了一个方法中明确具体元素类型的getMax_1。
      2 由于getMax_1无法获取其他类型元素的最大值。提高扩展性,操作所有对象。
            就升级为了Object 。getMax_2
      3 因为定义成Object,什么类型都可以接收,容易出现运行时的ClassCastException
            所以使用泛型,对要操作的元素进行类型的限定。
  思路:
  1.获取元素最大值的方法,通常是拿一个对象作为最大值,和遍历出的每一个元素比对,只要比这个最大值大即
  赋值给最大值.
  2.按照题目要求,第一步,首先定义一个方法中明确具体元素类型的getMax_1方法.
  3.那么就需要指定泛型,来对方法的参数类型做限定
技术分享
public class GenericTest3 {

    public static void main(String[] args) {
        //1.创建一个集合Collection
        Collection<Student> c1 = new ArrayList<Student>();
        c1.add(new Student("xiaoming",5));
        c1.add(new Student("zhangsan",15));
        c1.add(new Student("lisi",7));
        c1.add(new Student("xia",12));

        //2.定义一个方法,明确具体元素类型为Student的方法getMax_1
        Student stuMax = getMax_1(c1);
        System.out.println("Student中的最大值"+stuMax);

        //3.由于getMax_1的方法,只能传入Student类型的元素,扩展性不好.重写方法getMax_2
        //用Obeject类型,可以传入任何类型的参数
        Collection objList = new ArrayList();
        objList.add(new Worker("xiaolu",35));
        objList.add(new Worker("luf",25));
        objList.add(new Worker("xuze",28));
//        objList.add("string");    方法2是解决了类型的局限性,甚至1个集合传入两个数据类型,造成运行时异常
        Object objMax = getMax_2(objList);
        System.out.println("Object中的最大值"+objMax);

        //4.为了解决方法1的局限性,方法2的不稳定因素,方法3加入泛型,定义上限,下限
        Collection<Worker> c2 = new ArrayList<Worker>();
        c2.add(new Worker("xiaolu",35));
        c2.add(new Worker("luf",25));
        c2.add(new Worker("xuze",28));

        Student stumax3 = getMax_3(c1);
        Worker wokermax3 = getMax_3(c2);
        Object objmax3 = getMax_3(objList);
        System.out.println("Student中的最大值"+stumax3);
        System.out.println("Worker中的最大值"+wokermax3);
        System.out.println("Object中的最大值"+objmax3);

    }

    /**
     * 方法3:可以接收任何类型的集合取最大值的方法.
     * 注意:取最大值,需要comparTo方法的主持,所以传入的对象,必须具备比较方法,即必须实现Comparable
     * 接口的对象才行.或者说,这个对象类本身并不具备比较方法,但是其父类具备比较方法,那么可以
     * 这样限定<? super 需要用比较方法的那个对象>,这个限定放在什么位置?
     * 注意:静态方法,就不能把这个泛型参数放在类上了,而是放在方法上.
     * 其次,传入对象的类型可以是这个对象或者这个对象的子类型.<? extends 这个对象>
     *  
     */
    public static <T extends Comparable<? super T>> T getMax_3(Collection<? extends T> c1) {
        Iterator<? extends T> it = c1.iterator();
        T ifMax = it.next();
        while(it.hasNext()){
            T temp = it.next();
            if(temp.compareTo(ifMax)>0){
                ifMax = temp;
            }
        }
        return ifMax;
    }

    /**
     * 对方法1进行了改造,能接收所有类型的集合,求出最值并返回
     * 但是思考?还有没有弊端呢?    
     * @param objList
     * @return
     */
    public static Object getMax_2(Collection objList) {
        Iterator it = objList.iterator();
        Object ifMax = it.next();
        while(it.hasNext()){
            //此处temp必须用Comparable接口的子类型才行,因为需要compareTo方法
            Comparable temp =(Comparable) it.next();
            if(temp.compareTo(ifMax)>0){
                ifMax = temp;
            }
        }
        return ifMax;
    }

    /**
     * 按要求1完成
     * 求Student集合中最大值的方法
     * 但是有局限性,传入其他不是Student类型的集合无法获取最大值.
     * @param c1    限定传入Student类型的Collection集合.
     * @return    Student     返回Student类型的最大值.
     */
    public static Student getMax_1(Collection<Student> c1) {
        Iterator<Student> it = c1.iterator();
        Student ifMax = it.next();
        while(it.hasNext()){
            Student temp = it.next();
            if(temp.compareTo(ifMax)>0){
                ifMax = temp;
            }
        }
        return ifMax;
    }
}
View Code

 

泛型限定的练习。获取Collection集合中的最大值

标签:

原文地址:http://www.cnblogs.com/zyjcxc/p/5456478.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!