package Gather;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Test5 {
public static void main(String[] args) {
List list = new ArrayList();
list.add(“b”);
list.add(“a”);
list.add(“a”);
list.add(“c”);
list.add(“1”);
System.out.println(list);
Collections.reverse(list); //翻转集合中的元素
System.out.println(list);
Collections.shuffle(list); //对集合中的元素进行随机排序
System.out.println(list);
Collections.sort(list); //list集合字典升序排序
System.out.println(list);
System.out.println(Collections.frequency(list,"a"));
//返回指定集合中指定元素的出现次数
Collections.replaceAll(list,"a","aa");
System.out.println(list);
使用新值替换List对象的所有旧值
// Collections.swap(list,0,3); //将指定list 集合中的i处元素和j处元素进行交换
// System.out.println(list);
// System.out.println(Collections.max(list));//返回集合中的最大值
// System.out.println(Collections.min(list));//返回集合中的最小值
//
// Student s = new Student(“tom”,18);
// Student s1 = new Student(“bob”,20);
// Student s2 = new Student(“alec”,19);
//
// List stus = new ArrayList();
// stus.add(s);
// stus.add(s1);
// stus.add(s2);
//
// Student ss = Collections.max(stus,new Student());
// System.out.println(ss.name + “,”+ss.age); //获取年龄最大那个值
// Student ss1 = Collections.min(stus,new Student());
// System.out.println(ss1.name + “,”+ss1.age); //获取年龄最大那个值
// for ( Student stu : stus){
// System.out.println(stu.age + " "+stu.name);
// }
// Collections.sort(stus,new Student());
// System.out.println(“使用工具排序后”);
// for ( Student stu : stus){
// System.out.println(stu.age + " "+stu.name);
// }
}
}
class Student implements Comparator{
int age;
String name;
public Student(){
}
public Student (String name , int age){
this.name =name ;
this.age = age ;
}
@Override
public int compare(Student o1, Student o2) {
if (o1.age > o2.age){
return 1;
}else if (o1.age<o2.age){
return -1;
}else {
return 0;
}
}
}