Java集合框架之Collections⼯具类

Java集合框架之Collections⼯具类

  • Collections⼯具类
    • Java⾥关于集合的⼯具类,包含有各种有关集合操作的静态多态⽅法,不能实例化(把构造函
      数私有化)
public class Collections {
    
    
 // Suppresses default constructor, ensuring noninstantiability.
 private Collections() {
    
    
 }
  • 和Collection的区别

    • Collection是接⼝,提供了对集合对象进⾏基本操作的通⽤接⼝⽅法,List、Set等多种具体的实现类
    • Collections是⼯具类,专⻔操作Collection接⼝实现类⾥⾯的元素
  • 常见方法

    • 排序 sort(List list)

      • 按⾃然排序的升序排序

      List list = new ArrayList<>();
      list.add(“aaaa”);
      list.add(“zzz”);
      list.add(“gggg”);
      System.out.println(list);
      Collections.sort(list);
      System.out.println(list);

      • sort(List list, Comparator c) ⾃定义排序规则,由Comparator控制排序逻辑

      List list = new ArrayList<>();
      list.add(“aaaa”);
      list.add(“zzz”);
      list.add(“gggg”);
      System.out.println(list);
      //默认升序
      Collections.sort(list, Comparator.naturalOrder());
      System.out.println(list);
      //降序
      Collections.sort(list, Comparator.reverseOrder());
      System.out.println(list);

    • 随机排序 shuffle(List list)

    List list = new ArrayList<>();
    list.add(“1”);
    list.add(“2”);
    list.add(“3”);
    list.add(“4”);
    list.add(“5”);
    list.add(“6”);
    list.add(“7”);
    list.add(“8”);
    list.add(“9”);
    list.add(“10”);
    list.add(“J”);
    list.add(“Q”);
    list.add(“K”);
    System.out.println(list);
    Collections.shuffle(list);
    System.out.println(list);

  • 获取最⼤元素 max(Collection coll) 默认⽐较,不适合对象⽐较

  • 获取最⼤元素 max(Collection coll, Comparator comparator)

package domee.chapter9_1;

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

public class CollectionTest {
    
    
    public static void main(String[] args) {
    
    

        List<Student> list = new ArrayList<>();
        list.add(new Student("赵一",1));
        list.add(new Student("钱二",2));
        list.add(new Student("孙三",3));
        list.add(new Student("李四",4));
        list.add(new Student("周五",5));
        list.add(new Student("吴六",6));
        list.add(new Student("郑七",7));
        list.add(new Student("王浩然",8));
        Collections.shuffle(list);
        System.out.println(list.toString());

        Student student1 =  Collections.max(list, new Comparator<Student>() {
    
    
            @Override
            public int compare(Student o1, Student o2) {
    
    
                return o1.getAge()-o2.getAge();
            }
        });

        System.out.println(student1);

        Student student2 = Collections.min(list, new Comparator<Student>() {
    
    
            @Override
            public int compare(Student o1, Student o2) {
    
    
                return o1.getAge() - o2.getAge();
            }
        });

        System.out.println(student2);

        Collections.sort(list);

        System.out.println(list);


    }
}
class Student implements Comparable{
    
    
    private int age;
    private String name;

    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+
                '}';
    }

    @Override
    public int compareTo(Object o) {
    
    

        Student student = (Student) o;
        return this.age-student.age;
    }
}

  • 获取最⼩元素 min(Collection coll)
  • 创建不可变集合unmodifiablleXXX()
List<String> list = new ArrayList<>();
 list.add("SpringBoot课程");
 list.add("架构课程");
 list.add("微服务SpringCloud课程"); //设置为只读List集合
 list = Collections.unmodifiableList(list);
 System.out.println(list);
 Set<String> set = new HashSet<>();
 set.add("Mysql教程");
 set.add("Linux服务器器教程");
 set.add("Git教程");
 //设置为只读Set集合
 set = Collections.unmodifiableSet(set);
 System.out.println(set);
 Map<String, String> map = new HashMap<>();
 map.put("key1", "课程1");
 map.put("key2", "课程2");
 //设置为只读Map集合
 map = Collections.unmodifiableMap(map);
 System.out.println(map);

猜你喜欢

转载自blog.csdn.net/ruan_luqingnian/article/details/113646402