Java-Collections工具类测试

package collection;

import java.util.*;

public class CollectionsTest {

    //通过Collections工具类中的sort方法对Integer泛型的List排序
    public void testSort1()
    {
        List<Integer> integerList = new ArrayList<Integer>();
        Random random = new Random();
        Integer k;
        for(int i = 0;i < 10;i++)
        {
            //通过nestInt()添加十个100内的不重复整数
            k = random.nextInt(100);
            if(integerList.contains(k) == false)
            {
                integerList.add(k);
            }
            System.out.println("成功添加整数:"+k);
        }
        System.out.println("--------------排序前-------------");
        for(Integer integer:integerList)
        {
            System.out.println(integer);
        }

        Collections.sort(integerList);

        System.out.println("--------------排序后-------------");
        for(Integer integer:integerList)
        {
            System.out.println(integer);
        }
    }

    //通过Collections的sort方法对String泛型的List排序
    public void testSort2()
    {
        List<String> stringList = new ArrayList<String>();

        Random random = new Random();

        //生成十条长度为10以内的随机字符串

        String numbersAndLetters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

        for(int i = 0;i < 10;i++)
        {
            int num = random.nextInt(10);
            char[] ch = new char[num];
            int k;
            for(int j = 0;j < num;j++)
            {
                k = random.nextInt(62);
                ch[j] = numbersAndLetters.charAt(k);
            }

            //将字符数组转化为字符串
            StringBuffer sb = new StringBuffer();
            for(int m = 0;m < ch.length;m++)
            {
                sb.append(ch[m]);
            }
            String newStr = sb.toString();

            if(stringList.contains(newStr) == false && newStr.length() > 0)
            {
                stringList.add(newStr);
            }
            else
            {
                i--;
            }
        }

        System.out.println("--------------排序前-------------");
        for(String string:stringList)
        {
            System.out.println(string);
        }

        Collections.sort(stringList);

        System.out.println("--------------排序后-------------");
        for(String string:stringList)
        {
            System.out.println(string);
        }
    }

    //对Student类型泛型的List排序
    //Comparable接口:默认比较规则 Comparator接口:临时比较规则 对于一个对象不存在默认比较规则
    public void testSort3()
    {
        List<Student> studentList = new ArrayList<Student>();
        Random random = new Random();

        //生成三个1000内的随机不重复ID
        int k = 0;
        int[] arr = new int[3];
        while(k < 3)
        {
            boolean flag = true; 
            int rd = random.nextInt(1000);
            for(int i = 0;i < k; i++)
            {
                if(arr[i]==rd)
                {
                    flag = false;           
                }
            }
            if(flag)
            {
                arr[k] = rd;
                k++;
            }
        }

        studentList.add(new Student(arr[0]+"","Mike"));
        studentList.add(new Student(arr[1]+"","Lucy"));
        studentList.add(new Student(arr[2]+"","Jack"));
        studentList.add(new Student(10000+"","Tom"));
        System.out.println("--------------排序前-------------");
        for(Student student:studentList)
        {
            System.out.println("学生:"+student.id+":"+student.name);
        }

        Collections.sort(studentList);//要在Student类中实现Comparable接口

        System.out.println("--------------按ID排序后-------------");
        for(Student student:studentList)
        {
            System.out.println("学生:"+student.id+":"+student.name);
        }

        Collections.sort(studentList,new StudentComparator());//重写comparator中的compare方法
        System.out.println("--------------按姓名排序后-------------");
        for(Student student:studentList)
        {
            System.out.println("学生:"+student.id+":"+student.name);
        }
    }

    public static void main(String[] args) {
        CollectionsTest ct = new CollectionsTest();
        //ct.testSort1();
        //ct.testSort2();
        ct.testSort3();
    }

}
package collection;

import java.util.Comparator;

public class StudentComparator implements Comparator<Student> {

    @Override
    //o1大于o2 返回正数
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }

}
package collection;

import java.util.*;
//学生类
public class Student implements Comparable<Student> {
    public String id;
    public String name;
    public Set<Course> courses;


    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Student))
            return false;
        Student other = (Student) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
    public Student(String id,String name)
    {
        this.id = id;
        this.name = name;
        this.courses = new HashSet<Course>();
    }

    //按学生ID排序
    @Override
    public int compareTo(Student o) {
        // 相等返回0 this对象较大返回正值 反之返回负值
        return this.id.compareTo(o.id);
    }
}

你可能感兴趣的:(ArrayList)