理解:``Java⾥关于集合的⼯具类,包含有各种有关集合操作的静态多态⽅法,不能实例化(把构造函数私有化)
public class Collections {
// Suppresses default constructor, ensuring noninstantiability.
//不能new(实例化)
private Collections() {
}
1.和Collection的区别
1)Collection是接⼝,提供了对集合对象进⾏基本操作的通⽤接⼝⽅法,List、Set等多种具体的实现类
2)Collections是⼯具类,专⻔操作Collection接⼝实现类⾥⾯的元素
2.常⻅⽅法
1)排序 sort(List list)用于List
public static void test1(){
List <String> list = new ArrayList<>();
list.add("aaaa");
list.add("zzz");
list.add("gggg");
//默认升序
Collections.sort(list);
System.out.println(list);
}
//[aaaa, gggg, zzz]
2)sort(List list, Comparator c) ⾃定义排序规则,
由Comparator:比较器对象,控制排序逻辑
//默认升序
Collections.sort(list,Comparator.naturalOrder());
//降序
Collections.sort(list,Comparator.reverseOrder());
例:按学生年龄找出最大最小的学生
public static void test(){
List <Student>list = new ArrayList<>();
list.add(new Student("jack",89));
list.add(new Student("tom",90));
list.add(new Student("Bob",25));
list.add(new Student("lucy",45));
list.add(new Student("tomm",23));
list.add(new Student("qwe",12));
// System.out.println(list.toString());
//[Student{name='jack', age=89}, Student{name='tom', age=90}, Student{name='Bob', age=25},
// Student{name='lucy', age=45},
// Student{name='tomm', age=23}, Student{name='qwe', age=12}]
Student student1 = Collections.max(list, new Comparator<Student>() {
@Override
//参数1-2
public int compare(Student o1, Student o2) {
return o1.getAge() - o2.getAge();
}
});
Student student2 = Collections.min(list, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getAge() - o2.getAge();
}
});
System.out.println(student1);
System.out.println(student2);
class Student {
public Student(String name, int age) {
this.name = name;
this.age = age;
}
private int age;
private String name;
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
2)创建不可变集合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);
3.随机排序 shuffle(List list)
public static void test1(){
List <String> 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");
Collections.shuffle(list);
System.out.println(list);
//[5, 10, 7, 2, 9, Q, 8, 4, J, 3, 6, 1]
//[Q, J, 10, 2, 5, 7, 3, 6, 8, 1, 4, 9]
}
对比comparable排序接口
什么是Comparable
public interface Comparable<T> {
public int compareTo(T o);
}
1)是⼀个接⼝,定制排序规则
2)对实现它的每个类的对象进⾏整体排序,⾥⾯ compareTo ⽅法是实现排序的具体⽅法
3)⽐如TreeSet、SortedSet、Collections.sort() ⽅法调⽤进⾏排序
4)String、Integer等类默认实现了这个接⼝,所以可以排序(看源码)
5)集合里面的元素也叫对象
2.详解compareTo⽅法(固定排序规则)
⽤于⽐较次对象和指定对象的顺序,o为要⽐较的对象
返回int类型
1)⼤于0, 表示this⼤于传进来的对象o ,则往后排,即升序
2)等于0,表示this等于传进来的对象o
3)⼩于0,表示this⼩于传进来的对象o
package Collections;
public class Student implements Comparable{
private String name;
private int age;
public int getAge() {
return age;
}
public Student(String name,int age){
this.age=age;
this.name=name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// 打印推荐
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int compareTo(Object o) {
if(o instanceof Student){
Student student = (Student)o;
return this.age - student.age;
}
//返回的数是0代表两个对象相同
return 0;
}
}
package Collections;
import java.util.*;
public class TestCom {
public static void main(String[] args) {
Set<Student> studentSet = new TreeSet<>();
studentSet.add(new Student("jack", 32));
studentSet.add(new Student("tom", 22));
studentSet.add(new Student("mary", 35));
studentSet.add(new Student("tim", 11));
studentSet.add(new Student("tony", 49));
studentSet.add(new Student("dd", 30));
System.out.println(studentSet);
}
}