CollectionsDemo1+2 List集合普通元素的排序,自定义类型的排序

CollectionsDemo1 List集合普通元素的排序,自然排序

/**
 * List集合的排序
 * 排序List集合使用集合的工具类Collections的静态方法sort,
 * 该方法可以对指定List集合进行自然排序(从小到大)
 */
public class SortDemo1 {

    public static void main(String[] args) {
        //1,生成一个List<Insteger>集合
        List<Integer> list = new ArrayList<Integer>();

        //2.为集合添加随机10个随机整数,作为元素
        Random rand = new Random();
        for(int i=0;i<10;i++){
            list.add(rand.nextInt(100));
        }

        //3.输出集合,查看list内的元素
        System.out.println(list);    //[90, 71, 17, 57, 90, 69, 48, 82, 31, 41] 

        //4.对集合进行排序,用Collections类的静态方法之一 sort
        Collections.sort(list);

        //5.输出集合,查看list内的元素
        System.out.println(list);    //[12, 12, 20, 27, 33, 58, 60, 66, 70, 71]

        //6.针对总结2进行测试,排序字符串
        List<String> strList = new ArrayList<String>();
        strList.add("Mary");
        strList.add("Lee");
        strList.add("Varen");
        strList.add("Garen");
        strList.add("爸爸");
        strList.add("维恩");
        strList.add("阿满");
        strList.add("成吉思汗在北京");
        strList.add("成吉思汗");

        //7.输出结果
        System.out.println(strList);
        Collections.sort(strList);
        System.out.println(strList);
        /*
         * [Mary, Lee, Varen, Garen, 爸爸, 维恩, 阿满, 成吉思汗在北京, 成吉思汗]
           [Garen, Lee, Mary, Varen, 成吉思汗, 成吉思汗在北京, 爸爸, 维恩, 阿满]
         */

        //8.针对总结3,测试自定义元素能否用次方法排序
        List<Point> pointList = new ArrayList<Point>();
        pointList.add(new Point(3,7));
        pointList.add(new Point(3,5));
        pointList.add(new Point(5,2));
        pointList.add(new Point(4,3));

//        Collections.sort(pointList);    编译出错

    }
    /**
     * 总结
     * 1.只能根据Collections.sort方法的自然顺序排序,只能由小到大排序.
     * 2.想要排序的不是数值能排序么?按照什么顺序排序呢?
     *   测试结论:字符串也能排序,根据英文字母的ascii值,由小到大.中文则无规律.
     * 3.对自定义的类型元素,能排序么?
     *      测试结论:Collections.sort(pointList);编译报错.
     *      说明单独使用这个方法没法对自定义类型元素进行排序,需要另寻求解决办法
     */
}

CollectionsDemo2 List 自定义类型元素的排序

/**
 *    自定义类型元素的排序,用Collections的静态方法sort怎么实现呢?
 *    注意:这里首先自定义个类(Point),才能实验下面这个Demo
 *    关键点: 使用Comparator接口实现对集合中的元素排序
 *
     总结:
     1.排序自定义元素的方法,用Collections.sort可以实现.
     前提是,需要实现Comparator接口并实现compareTo方法,定义比较规则.
     2.但是,有一点弊端,就是为了实现Collections.sort方法,需要在我们
     自己的类中定义其他的方法,"侵入性"太强,有没有更好的解决办法呢?
    


 */
public class CollectionsDemo2 {

    public static void main(String[] args) {
        //1.建立自定义元素的集合List
        List<Point> list = new ArrayList<Point>();

        //2.在集合中添加Point类型的对象
        list.add(new Point(8,3));
        list.add(new Point(10,9));
        list.add(new Point(5,6));
        list.add(new Point(2,3));
        list.add(new Point(1,5));
        System.out.println(list);    //输出未排序结果
//        [Point [x=8, y=3], Point [x=10, y=9], Point [x=5, y=6], Point [x=2, y=3], Point [x=1, y=5]]

        //3.我们知道,用Collections.sort(list)进行排序,会报错
        /*关键点: 使用Comparator接口实现对集合中的元素排序
          首先Point类需要实现Comparator接口,并实现其compareTo的抽象方法.
          这里需要注意, Compartor<T>接口也是可以指定泛型的.一般定义成跟类同类型.
         compareTo(指定对象)方法 返回值int   
         返回值>0  这个对象大于指定对象
         返回值<0 这个对象小于指定对象
         返回值=0 这个对象等于指定对象
        */
        Collections.sort(list);        //再Point类重写compareTo方法后,Collections.sort编译通过
//      [Point [x=2, y=3], Point [x=1, y=5], Point [x=5, y=6], Point [x=8, y=3], Point [x=10, y=9]]
        System.out.println(list);    //输出排序后结果
    }
}

实验用Point类

public class Point implements Comparable<Point>{
    private int x;
    private int y;

    public Point(int x, int y) {
        super();
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }

    @Override
    public String toString() {
        return "Point [x=" + x + ", y=" + y + "]";
    }

    @Override
    public int compareTo(Point o) {
        /*
         * 坐标点的比大小,比该点到远点的距离,即比两个坐标点 x*x+y*y 的大小
         * */
        int tLeng = this.x*this.x + this.y*this.y;
        int oLeng = o.x*o.x + o.y*o.y;

        return tLeng - oLeng;
    }
}
View Code

 

posted @ 2016-04-30 14:56  安仔80  阅读(365)  评论(0编辑  收藏  举报