《JAVA集合框架中的Collections类》

来源:互联网 发布:查找算法java 编辑:程序博客网 时间:2024/04/29 03:39

Collections 工具类(java.util.collections)

它里边定义的一些方法都是操作collections对象的,其本身也是java集合框架的一个成员,跟Map,List,都是并列的。

下面通过一个小例子来帮助理解collections中的sort()方法

package Collections_Chen;

import java.util.Set;

import java.util.HashSet;

public classStudent implementsComparable<Student>{

    private Stringid;

    private Stringname;

    public String get_id(){

        returnid;

    }

    public String get_name(){

        returnname;

    }

    public Student(Stringi,String n){

        id=i;

        name=n;

    }

    @Override

    public int compareTo(Student o) {//当这个o对象和当前的Student对象相等的时候就会返回一个0

        // TODO Auto-generated method stub

        //return 0;

        //现在我们来这样写

        return this.id.compareTo(o.id);//表示用当前的id和比较参数对象的id去进行比较。将两个对象的id比较结果作为对象的比较结构

    }

}

 

 

package Collections_Chen;

 

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

import java.util.List;

import java.util.Random;

 

/*

 * 将要完成

 * 1,通过Collections.sort()方法,对Integer泛型的List进行排序;

 * 2,对String泛型的List进行排序;

 * 3对其它类型的泛型进行排序,以Student为例

 *

 */

public classCollectionsTest {

    /*

     * 1,通过Collections.sort()方法,对Integer泛型的List进行排序;

     * 创建一个Integer泛型的List,插入10100以内的不重复的随机数,调用Collections.sort()方法对其进行排序

     */

   

    void TestSort1(){

        List<Integer>integerList=newArrayList<Integer>();

        Random random=new Random();

        Integerk;

        for(inti=0;i<10;i++){

            do{

                k=random.nextInt(100);

            }while(integerList.contains(k));//调用其中的contains方法是为了产生的k值不重复

            integerList.add(k);

            System.out.println("成功添加整数:"+k);

        }

        System.out.println("--------排序前----------");

        for(Integerinteger:integerList){

            System.out.println("元素:"+integer);

        }

        Collections.sort(integerList);

        System.out.println("--------排序后----------");

        for(Integerinteger:integerList){

            System.out.println("元素:"+integer);

        }

    }

   

    /*

     * 2,对String泛型的List进行排序;

     * 创建String泛型的List,添加三个乱序的String元素

     * 调用sort方法,再次输出排序后的顺序

     *

     */

    void TestSort2(){

        List<String>stringList=newArrayList<String>();

        stringList.add("google");

        stringList.add("lenovo");

        stringList.add("Microsoft");

        System.out.println("--------排序前----------");

        for(Stringstring:stringList){

            System.out.println("元素:"+string);

        }

        Collections.sort(stringList);

        System.out.println("--------排序后----------");

        for(Stringstring:stringList){

            System.out.println("元素:"+string);

        }

    }

    //3对其它类型的泛型进行排序,以Student为例

    public void testSort3(){

        List<Student>studentList=newArrayList<Student>();

        Randomrandom=new Random();//产生三个随机的正整数id

        studentList.add(new Student(random.nextInt(1000)+"","Lucy"));//产生1000以内的随机数作为id

        studentList.add(new Student(random.nextInt(1000)+"","Angela"));

        studentList.add(new Student(random.nextInt(1000)+"","Mike"));

        System.out.println("----------------排序前---------------------");

        for(Studentstudent:studentList){

            System.out.println("学生:"+student.get_id()+":"+student.get_name());

        }

        Collections.sort(studentList);//写到这个地方会发现sort处报错,这个问题你可以查看sortapi;你会发现如果想使用Collections.sort()方法对某个序列

        //进行排序,那么这个序列中的元素必须得实现comparable接口(这里student类并没有实现Comparable接口,所以我们需要去Student类中实现该接口)

        /*

         * 下面来说一说comparable接口

         * 这个comparable就相当于是给对象定义了某个默认的排序规则

         * Comparable接口---可比较的

         * 实现该接口表示:这个类的实例可以比较大小,可以进行自然排序

         * 定义了默认的比较规则

         * 其实现类需要实现compareTo()方法

         * compareTo()方法返回正表示大,返回负表示小,返回0表示相等

         *

         * Comparator接口----比较工具接口

         * 用于定义临时比较规则,而不是m默认比较规则

         * 其实现类需要实现compareTo()方法

         * ComparatorComparetor都是Java集合框架的成员

         */

        System.out.println("----------------排序后---------------------");

        for(Studentstudent:studentList){

            System.out.println("学生:"+student.get_id()+":"+student.get_name());

        }

    }

    public static void main(String[] args) {

        CollectionsTestct=new CollectionsTest();

        //ct.TestSort1();

        //ct.TestSort2();

        ct.testSort3();

    }

 

}

 

0 0