Java-Collections集合常用方法
Collections集合常用操作方法;
public static <T> boolean addAll(Collection<T>,T...elements):向集合中一次添加任意数量元素
public static void shuffle(List<?> list):打乱集合顺序
public static <T> void sort(List<T> list):将集合中的元素按照默认规则排序
1.已经定义好的类,已经实现并重写Comparable接口中的compareTo方法可以直接使用,默认升序排列
2.自定义类型,需要实现并重写Comparable接口(java.lang包)中的compareTo方法
int compareTo(T o)将此对象与指定的对象进行比较以进行排序。
*this.stuAge-o.stuAge:升序排列
*o.stuAge-this.age:降序排列
public static <T> void sort(List<T> list,Comparator<? super T>):将集合中指定元素按照指定规则排序
1.已经定义好的类和自定义类型,都需要实现并重写Comparable接口(java.util包)中的compare方法
int compare(T o1, T o2):比较其两个参数的顺序
*o1.stuAge-o2.stuAge:升序排列
*o2.stuAge-o1.age:降序排列
public class CollectionMethodDemo {
public static void main(String[] args) {
Collection<String> col=new ArrayList<>();
Collections.addAll(col, "abc","def","ghi");
System.out.println(col);
ArrayList<String> al1=new ArrayList<>();
Collections.addAll(al1, "abc","def","ghi");
Collections.shuffle(al1);
System.out.println(al1);
ArrayList<Integer> al2= new ArrayList<>();
Collections.addAll(al2,2,1,4,3);
System.out.println(al2);
Collections.sort(al2);
System.out.println(al2);
ArrayList<String> al3= new ArrayList<>();
Collections.addAll(al3,"cd","ab","efg");
System.out.println(al3);
Collections.sort(al3);
System.out.println(al3);
Student stu1 = new Student("张三",18);
Student stu2 = new Student("李四",16);
Student stu3 = new Student("王五",19);
ArrayList<Student> al4= new ArrayList<>();
Collections.addAll(al4, stu1,stu2,stu3);
System.out.println(al4);
Collections.sort(al4);
System.out.println(al4);
ArrayList<Integer> al5= new ArrayList<>();
Collections.addAll(al5,2,1,4,3);
System.out.println(al5);
Collections.sort(al5,new Comparator<Integer>(){
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
System.out.println(al2);
Student stu01 = new Student("张三",18);
Student stu02 = new Student("李四",16);
Student stu03 = new Student("王五",19);
ArrayList<Student> al6= new ArrayList<>();
Collections.addAll(al6, stu01,stu02,stu03);
Collections.sort(al6,new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.getStuAge()-o1.getStuAge();
}
});
System.out.println(al6);
Student stu04 = new Student("张三",18);
Student stu05 = new Student("李四",16);
Student stu06 = new Student("王五",19);
Student stu07 = new Student("王五",18);
ArrayList<Student> al7= new ArrayList<>();
Collections.addAll(al7, stu04,stu05,stu06,stu07);
Collections.sort(al7,new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int a = o1.getStuAge()-o2.getStuAge();
if(a==0){
a=o1.getStuName().charAt(0)-o2.getStuName().charAt(0);
}
return a;
}
});
System.out.println(al7);
}
}
class Student implements Comparable<Student>{
private String stuName;
private int stuAge;
public Student() {
}
public Student(String stuName, int stuAge) {
super();
this.stuName = stuName;
this.stuAge = stuAge;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
@Override
public String toString() {
return "Student [stuName=" + stuName + ", stuAge=" + stuAge + "]";
}
@Override
public int compareTo(Student o) {
return o.stuAge-this.stuAge;
}
}