Collections集合工具类
addAll和shuffle方法
代码示例
/*
public static <T> boolean addAll(collection<T> c,T...elements):往集合添加多个元素
public static void shuffle(List<?> list) 打乱顺序:打乱集合顺序
*/
public class aa {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
//往集合添加多个元素
list.add("a");
list.add("b");
list.add("c"); //要一个一个加
//public static <T> boolean addAll(collection<T> c,T...elements):往集合添加多个元素
Collections.addAll(list,"a","b","c"); //它的参数是可变参数,可以传递任意多个元素
System.out.println(list); //[a, b, c, a, b, c]
//public static void shuffle(List<?> list) 打乱顺序:打乱集合顺序
Collections.shuffle(list);
System.out.println(list); //[c, a, b, b, c, a] 每次重复的都不一样
}
}
sort方法1
代码示例
/*
public static <T> void sort(List<T> list):将集合中元素按照,默认规则排序 (里面只能传递list集合不能set)
注意事项:
sort(List<T> list)使用前提 (传递的是自定义类型的话)
被排序的集合里面存储的元素,必须实现Comparable接口,重写接口中的方法compareTo定义排序的规则
Comparable排序规则:
自己(this)-参数;升序
*/
public class bb {
public static void main(String[] args) {
ArrayList<Integer> list01 = new ArrayList<>();
list01.add(1);
list01.add(3);
list01.add(2);
System.out.println(list01);//[1, 3, 2]
//public static <T> void sort(List<T> list):将集合中元素按照,默认规则排序
Collections.sort(list01); // 默认是升序
System.out.println(list01); //[1, 2, 3]
ArrayList<String> list02 = new ArrayList<>();
Collections.addAll(list02,"a","c","b");
System.out.println(list02); //[a, c, b]
Collections.sort(list02); // 默认是升序
System.out.println(list02); //[a, b, c]
//对自定义类型排序
ArrayList<person> list03 = new ArrayList<>();
list03.add(new person("张三",18));
list03.add(new person("李四",20));
list03.add(new person("王五",15));
System.out.println(list03);//[person{name='张三', age=18}, person{name='李四', age=20}, person{name='王五', age=15}]
Collections.sort(list03);
System.out.println(list03);//[person{name='王五', age=15}, person{name='张三', age=18}, person{name='李四', age=20}]
}
}
public class person implements Comparable<person>{
private String name;
private int age;
//重写排序的规则
@Override
public int compareTo(person o) {
// return 0; //认为元素都是相同的
//自定义比较的规则,比较两个人的年龄(this,person)
return this.getAge()-o.getAge(); //年龄升序排序
// return o.getAge()-this.getAge(); //年龄降序排序
}
public person() {
}
public person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
sort方法2
代码示例
public class cc {
public static void main(String[] args) {
ArrayList<Integer> list01 = new ArrayList<>();
list01.add(1);
list01.add(3);
list01.add(2);
System.out.println(list01);//[1, 3, 2]
Collections.sort(list01, new Comparator<Integer>() { //使用匿名内部类
//重写比较的规则
@Override
public int compare(Integer o1, Integer o2) {
// return o1-o2;//升序
return o2-o1;//降序
}
});
System.out.println(list01); //[3, 2, 1]
//比较自定义类型
ArrayList<Student> list02 = new ArrayList<>();
list02.add(new Student("迪丽热巴",18));
list02.add(new Student("古力娜扎",20));
list02.add(new Student("杨幂",17));
System.out.println(list02);
//[Student{name='迪丽热巴', age=18}, Student{name='古力娜扎', age=20}, Student{name='杨幂', age=17}]
Collections.sort(list02, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
//按照年龄升序排序
return o1.getAge()- o2.getAge();
}
});
System.out.println(list02);
//[Student{name='杨幂', age=17}, Student{name='迪丽热巴', age=18}, Student{name='古力娜扎', age=20}]
}
}
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
标签:难点,Java,name,age,person,add,Collections,Student,public
From: https://blog.51cto.com/u_15841797/5973425