欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Collections类的基础知识整理

程序员文章站 2024-01-13 18:24:46
...

Collections类是用来操作集合的工具类,当操作集合时,如果发现集合的方法不够用时,就可以在Collections类中寻找合适的方法。

常用方法

①public static <T> boolean addAll(Collection<T> c,T……elements):往集合中添加一些元素;

②public static void shuffle(List<T> list):打乱集合的顺序;

③public static <T> void sort(List<T> list ):将集合中的元素按照默认的规则排序;

④public static <T> void sort(List<T> list,Comparator<? super T> c):将集合中的元素按照指定的规则进行排序;

⑤public static <T> int binarySearch(List<T> list,T key):用二分法查找集合中指定元素的索引,该方法的使用前提是集合已经排好序了,如果集合没有排序或者集合中没有该元素,则结果会返回一个负数索引;

⑥public static void reverse(List<T> list):将集合中的元素顺序颠倒过来(集合反转)。

/**
 * @description: 测试类
 * @author: Murphy
 * @date: 2020/7/37:44 上午
 */
public class Demo {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        Collections.addAll(list, 2, 1, 4, 3, 5);
        System.out.println("1---" + list);
        Collections.shuffle(list);
        System.out.println("2--- " + list);
        Collections.sort(list);
        System.out.println("3--- " + list);
    }
  
//输出结果
1---[2, 1, 4, 3, 5]
2--- [3, 1, 5, 4, 2]
3--- [1, 2, 3, 4, 5]

Comparator比较器

在上述public static <T> void sort(List<T> list,Comparator<? super T> c)方法中,就涉及到了Comparator这个接口,该接口代表一个比较器,排序是该接口的功能之一,比较的方法是:

public int compare(String o1,String o2):比较两个参数的顺序,其中o1-o2表示升序,o2-o1表示降序。

/**
 * @description: 创建一个学生类
 * @author: Murphy
 * @date: 2020/7/153:05 上午
 */
public class Student {
    private String name;
    private int age;

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

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

/**
 * @description: 测试类
 * @author: Murphy
 * @date: 2020/7/37:44 上午
 */
public class Demo {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        Student student1 = new Student("张三", 20);
        Student student2 = new Student("李四", 18);
        Student student3 = new Student("王五", 30);
        Student student4 = new Student("陈六", 17);
        Collections.addAll(list, student1, student2, student3, student4);
        System.out.println("list = " + list);
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge() - o2.getAge();
            }
        });
        System.out.println("list = " + list);
    }
  
//输出结果
list = [Student{name='张三', age=20}, Student{name='李四', age=18}, Student{name='王五', age=30}, Student{name='陈六', age=17}]
list = [Student{name='陈六', age=17}, Student{name='李四', age=18}, Student{name='张三', age=20}, Student{name='王五', age=30}]