Collection和Collections是啥关系?
Collection是集合接口
Collections是集合的操作类
Collections中常用的静态方法
sort()
1.可以对集合进行排序,不能对复杂的集合排序,需要先制定排序规则(重写compareTo()方法)
2.实现一个类的对象之间比较大小
实现步骤
1.该类要重写Comparable接口的compareTo()方法
2.自定义比较规则
返回值:相等,返回0;大于,返回1;小于,返回-1
//实现Comparable接口
public class Student implements Comparable
//重写compareTo()方法
public int compareTo(Object Object){
Student student = (Student)Object;//操作前必须强制转型
if(this.sid<student.sid){
return -1;
}else if(this.sid>student.sid){
return 1;
}else {
return 0;
}
}
2.调用sort()方法
//使用Collections中的sort()方法进行排序
Collections.sort(stuSet);
//for循环遍历集合
for (int i=0;i<stuSet.size();i++){
Student student = (Student) stuSet.get(i);
System.out.println(student.getSid()+"--"+student.getName());
}
max()/min()
binarySearch()
reverse()