Collections&Comparator&Comparable类API
程序员文章站
2022-04-15 17:49:33
//向集合中添加元素,可以是泛型的集合形式的,也可以是T的可变参数(多个T类型数据,或者T类型数组)public static boolean AddAll(Collection c, T... elements)//将列表乱序public static shuffle(List> list)//将列表按照默认规则排序public static void sort(List list)//将...
// 向集合中添加元素,可以是泛型的集合形式的,也可以是T的可变参数(多个T类型数据,或者T类型数组)
public static <T> boolean AddAll(Collection<T> c, T... elements)
// 将列表乱序
public static shuffle(List<?> list)
// 将列表按照默认规则排序
public static <T> void sort(List<T> list)
// 将列表按照给定规则排序
public static <T> void sort(List<T> list, Comparator<? super T>)
// Comparator排序需要实现的方法,o1-o2是升序;o2-o1是降序
public int compare(String o1, String o2)
使用Comparator排序
// 一个对象
public class Student() {
private String name;
private int age;
public Studnet() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
// 测试
// o1 - o2升序;o2 - o1降序
public void Demo() {
public static void main(String[] args) {
List students = new ArrayList<String>();
students.add(new Student("StudentA", 10));
students.add(new Student("StudentD", 40));
students.add(new Student("StudentC", 30));
students.add(new Student("StudentE", 50));
students.add(new Student("StudentB", 20));
Collections.sort(students, Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.getAge() - o1.getAge();
}
});
for(Student student : students) {
System.out.println(student);
}
// Student{name='StudentE', age=50}
// Student{name='StudentD', age=40}
// Student{name='StudentC', age=30}
// Student{name='StudentB', age=20}
// Student{name='StudentA', age=10}
}
}
使用Comparable排序
// 一个对象
public class Student() implements Comparable<Student>{
private String name;
private int age;
public Studnet() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int compareTo(String o) {
// this-o是升序
// o-this是降序
return this.age - o.age;
}
}
// 测试
public void Demo() {
public static void main(String[] args) {
List students = new ArrayList<String>();
students.add(new Student("StudentA", 10));
students.add(new Student("StudentD", 40));
students.add(new Student("StudentC", 30));
students.add(new Student("StudentE", 50));
students.add(new Student("StudentB", 20));
Collections.sort(students);
for(Student student : students) {
System.out.println(student);
}
// Student{name='StudentE', age=50}
// Student{name='StudentD', age=40}
// Student{name='StudentC', age=30}
// Student{name='StudentB', age=20}
// Student{name='StudentA', age=10}
}
}
本文地址:https://blog.csdn.net/fytvewrewr/article/details/109828104