一. Collections:针对集合操作工具类
1.提供静态功能:
public static void reverse(List<?> list):对List集合顺序反转
public static void shuffle(List<?> list):随机置换
2.
public static <T extends Comparable<? super T>> void sort(List<T> list):按照自然升序排序
(针对List集合排序)
public static <T> void sort(List<T> list,Comparator<? super T> c):按照比较器排序针对List集合
List<Student>:针对List进行自然升序排序,
主要条件:按照学生的年龄从小到大排
次要条件:年龄相同,比较姓名内容是否相同
public class Student_1 implements Comparable<Student_1> {
private String name;
private int age;
public Student_1() {
}
public Student_1(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_1{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int compareTo(Student_1 s) {
int num = this.age-s.age;
int num2 = (num==0)?(this.name.compareTo(s.name)):num;
return num2;
}
}
import java.util.List;
public class Collectionstest {
public static void main(String[] args) {
List<Student_1> list =new ArrayList<>();
Student_1 s1 = new Student_1("make",20);
Student_1 s2 = new Student_1("jone",20);
Student_1 s3 = new Student_1("fanxi",27);
list.add(s1);
list.add(s2);
list.add(s3);
Collections.sort(list);
for (Student_1 s:list) {
System.out.println(s);
}
}
}