Collection集合方法补充
掌握继承接口重写排序方法
LinkedList<Integer> li=new LinkedList<Integer>();
li.add(20);
li.add(50);
li.add(40);
li.add(10);
li.add(30);
Collections.reverse(li);
Collections.sort(li);
Collections.shuffle(li);
对ArrayList中的信息进行排序
public class Demo2 {
public static void main(String[] args) {
ArrayList<Student> arr=new ArrayList<Student>();
Student s1=new Student("lqx",52);
Student s2=new Student("wzx",52);
Student s3=new Student("yyl",32);
Student s4=new Student("zdi",38);
arr.add(s1);
arr.add(s2);
arr.add(s3);
arr.add(s4);
Collections.sort(arr, new Comparator<Student>(){
public int compare(Student s1,Student s2) {
int num=s1.getAge()-s2.getAge();
int num2=num==0?s1.getName().compareTo(s2.getName()):num;
return num2;
}
});
for(Student s:arr) {
System.out.println(s.getName()+", "+s.getAge());
}
}
}