Collections 工具类

Collections 工具类提供的几个常用的方法:addAll ,shuffle,sort

下面是实例:

package com.collectiondo;

import java.util.ArrayList;
import java.util.Collections;

//测试Collections 工具类
public class TestCollectionUtil {
    public static void main(String[] args) {
        //测试addAll
        ArrayList<String> alist = new ArrayList<>();
        Collections.addAll(alist,"a","b","e","g","c","d");
        System.out.println("alist:"+alist);

        //打乱顺序
        Collections.shuffle(alist);
        System.out.println("打乱顺序后的alist:"+alist);

        //sort排序
        ArrayList<Integer> blist = new ArrayList<>();
        blist.add(1);
        blist.add(2);
        blist.add(3);
        blist.add(4);
        blist.add(5);
        Collections.sort(blist);
        Collections.sort(alist);
        System.out.println("排序后的alist:"+alist);
        System.out.println("排序后的blist:"+blist);

    }
}

输出的结果是:

   alist:[a, b, e, g, c, d]
   打乱顺序后的alist:[b, g, a, c, e, d]
   排序后的alist:[a, b, c, d, e, g]
   排序后的blist:[1, 2, 3, 4, 5]

 

排序的方法的扩展:

一般数据的排序:

package com.collectiondo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

//测试一般类型的排序和对象类型的排序
public class TestCollectionSort {
    public static void main(String[] args) {
        ArrayList<Integer> ilist=new ArrayList<>();
        ilist.add(1);
        ilist.add(12);
        ilist.add(221);
        ilist.add(4);
        ilist.add(9);
        ilist.add(543);

        System.out.println("排序前ilist:"+ilist);

        Collections.sort(ilist, new Comparator<Integer>() {
            //重写比较较规则
            @Override
            public int compare(Integer o1, Integer o2) {
               // return o1-o2; 升序
                 return o2-o1;  //降序
            }
        });

        System.out.println("排序后ilist:"+ilist);
    }

}

输出的结果是:

   排序前ilist:[1, 12, 221, 4, 9, 543]
   排序后ilist:[543, 221, 12, 9, 4, 1]

 

对象类型的排序:

package com.collectiondo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

//对象型集合的排序
class Student{
    private Integer age;
    private String name;

    public Integer getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    public Student(Integer age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

public class TestPojoSort {
    public static void main(String[] args) {
        ArrayList<Student> stuList=new ArrayList();
        Student stu1=new Student(19,"路飞");
        Student stu2=new Student(17,"索隆");
        Student stu3=new Student(21,"娜美");
        Student stu4=new Student(1,"乔巴");

        Collections.addAll(stuList,stu1,stu2,stu3,stu4);
        System.out.println("排序前:"+stuList);

        //现在要让集合按照对象中的年龄来排序
        Collections.sort(stuList, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge()-o2.getAge();
            }
        });

        //排序后的学生列表
        System.out.println("排序后:"+stuList);
    }
}

输出结果:

排序前:[Student{age=19, name='路飞'}, Student{age=17, name='索隆'}, Student{age=21, name='娜美'}, Student{age=1, name='乔巴'}] 排序后:[Student{age=1, name='乔巴'}, Student{age=17, name='索隆'}, Student{age=19, name='路飞'}, Student{age=21, name='娜美'}]

 

 

 

 

 

package com.collectiondo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

//对象型集合的排序
class Student{

private Integer age;
private String name;

public Integer getAge() {

return age;
}


public String getName() {
return name;
}


public Student(Integer age, String name) {
this.age = age;
this.name = name;
}


@Override
public String toString() {

return "Student{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}

}

public class TestPojoSort {
public static void main(String[] args) {
ArrayList<Student> stuList=new ArrayList();
Student stu1=new Student(19,"路飞");
Student stu2=new Student(17,"索隆");
Student stu3=new Student(21,"娜美");
Student stu4=new Student(1,"乔巴");

Collections.addAll(stuList,stu1,stu2,stu3,stu4);
System.out.println("排序前:"+stuList);

//现在要让集合按照对象中的年龄来排序
Collections.sort(stuList, new Comparator<Student>() {

@Override
public int compare(Student o1, Student o2) {

return o1.getAge()-o2.getAge();
}

});

//排序后的学生列表
System.out.println("排序后:"+stuList);
}

}
posted @ 2020-08-27 15:14  呆马and鸽子  阅读(211)  评论(0)    收藏  举报