import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
/**
* Collections操作集合, 集合工具类
*Collections.addAll(list,"a","b","c") 添加多个元素
*Collections.shuffle(list) 打乱元素
*Collection.sort(list) 默认为升序排序(从小到大)
* 被排序的集合中的数据类必须重写过Comparable的方法,int,String自带了,
* 其他类需要自己写
* 通过自己写的方式比较,重写comparator方法 new Comparator<Integer>()
*/
public class collections {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
//单个添加
list.add("c");
list.add("s");
list.add("b");
list.add("z");
/**
* 添加
*/
Collections.addAll(list,"a","b","c");
System.out.println("使用Collection.add 的添加方式\n"+list);
System.out.println();
/**
* 打乱
*/
Collections.shuffle(list);
System.out.println("Collections.shuffle 打乱集合 \n"+list);
System.out.println();
/**
* 排序
*/
Collections.sort(list);
System.out.println("使用java自带的排序 \n"+list);
System.out.println();
/**
* 对自定义的Comparable比较方法排序
* 重写比较规则 compareTo
*/
ArrayList<Person1> list1 = new ArrayList<>();
list1.add(new Person1("张三",20));
list1.add(new Person1("李四",19));
list1.add(new Person1("王五",21));
Collections.sort(list1); //通过年龄的比较
System.out.println("重写compareTo 比较年龄排序\n"+list1);
System.out.println();
/**
* Comparator : 找第三方的裁判,比较两个
*/
ArrayList<Integer> list2 = new ArrayList<>();
Collections.addAll(list2,1,3,5,6,8);
System.out.println(list2);
Collections.sort(list2, new Comparator<Integer>() {
@Override //重写Comparator<Integer>
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
System.out.println("comparator \n"+list2);
System.out.println();
/**
*Comparator , 用自定义的类型,重写comparator<student>
*</student>
*/
ArrayList<student> students = new ArrayList<>();
Collections.addAll(students,new student("迪丽热巴",20),
new student("古力娜扎",18),new student("杨幂",25));
Collections.sort(students, new Comparator<student>() {
@Override
public int compare(student o1, student o2) {
return o1.getAge()-o2.getAge();
}
});
System.out.println("学生类\n"+students);
System.out.println();
}
}
class student{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public student() {
}
public student(String name, int age) {
this.name = name;
this.age = age;
}
}
class Person1 implements Comparable<Person1>{
private String name;
private int age;
@Override
public String toString() {
return "Person1{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public Person1() {
}
public Person1(String name, int age) {
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Person1 o) {
// this.getAge() - o.getAge() 升序
return this.getAge() - o.getAge();
// o.getAge() - this.getAge()
}
}
Collections集合工具类,Comparator重写比较方法
最新推荐文章于 2024-07-29 16:35:40 发布