Collections集合工具类的常用方法
java.utils.Collections是集合工具类,用来对集合进行操作。
主要有以下几个常用方法:
- public static boolean addAll(Collection c, T… elements):往集合中添加一些元素。
- public static void shuffle(List<?> list) 打乱顺序:打乱集合顺序。
- public static void sort(List list):将集合中元素按照默认规则排序。
- public static void sort(List list,Comparator<? super T> ):将集合中元素按照指定规则排序。
一、public static boolean addAll(Collection c, T… elements)方法
此方法适用于所有Collection接口下的实现类集合
T…elements为可变参数,可存储任意数量的元素成员
public class Demo03 {
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<>();
Integer num01 = new Integer(1);
Integer num02 = new Integer(2);
Integer num03 = new Integer(3);
Collections.addAll(arrayList, num01, num02, num03);
System.out.println(arrayList);//[1, 2, 3]
}
}
二、public static void shuffle(List<?> list)方法
此方法仅限List接口下的实现类集合使用
因为List集合的特性有 “存取有序” 和 “有索引” 这个特点,所以才讲究去排序和乱序之类的(我这样理解)
public class Demo03 {
public static void main(String[] args) {
ArrayList<Student> arrayList = new ArrayList<>();
arrayList.add(new Student("a",1));
arrayList.add(new Student("b",2));
arrayList.add(new Student("c",3));
Collections.shuffle(arrayList);
System.out.println(arrayList);//[Student{name='b', age=2}, Student{name='a', age=1}, Student{name='c', age=3}]
}
}
记得重写自定义类的toString方法 ~
三、public static void sort(List list)方法
此方法同样仅限List接口下的实现类集合使用
public class Demo03 {
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<>();
Collections.addAll(arrayList, 1, 3, 2, 4, 6, 5);//默认升序排序
Collections.sort(arrayList);
System.out.println(arrayList);//[1, 2, 3, 4, 5, 6]
}
}
Collections.sort 方法是如何实现默认升序排序的?
从上图可以看到,此sort()方法中的所有元素都必须实现Comparable接口:里面只有int compareTo(T o)抽象方法。而像Integer、String等数据类型大多都默认实现了java.lang.Comparable接口下的int compareTo(T o)抽象方法。所以在用集合存储这些数据类型时,我们能够直接使用public static void sort(List list)方法对其进行排序。
但有两点要要注意:
- 自定义的数据类型(如自定义的Student类),需要手动实现Comparable接口的compareTo(T o)方法
public class Student implements Comparable<Student>{
private String name;
private int age;
@Override
public int compareTo(Student o) {
return this.getAge() - o.getAge(); //按年龄 “升序” 排序
// return o.getAge() - this.getAge() //按年龄 “降序” 排序
}
//其他方法...
}
- 使用此方法的JAVA自带数据类型只能根据元素的自然顺序 对指定列表按升序进行排序。
所以,想要进行灵活自定义的排序只能使用public static void sort(List list,Comparator<? super T> )方法(见下方法)
四、public static void sort(List list,Comparator<? super T> )方法
在JAVA中提供了两种比较实现的方式:
- 一种是比较死板的采用java.lang.Comparable接口去实现(上一个方法就是这样)
- 一种是使用可以灵活定义排序规则的java.util.Comparator接口完成
此方法使用的便是第二种接口实现的排序
从上图可知,我们可以通过向sort()方法传递一个内部匿名类(实现并重写了Comparator接口内的compare方法),来实现我们想要的自定义排序。
public class Demo03 {
public static void main(String[] args) {
ArrayList<Student> arrayList = new ArrayList<>();
arrayList.add(new Student("abc",1));
arrayList.add(new Student("abb",3));
arrayList.add(new Student("aa",2));
arrayList.add(new Student("b",2));
arrayList.add(new Student("c",1));
Collections.sort(arrayList, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if(o1.getName() == o2.getName()) { //先按名字升序排序,如果名字相同,则按年龄进行升序排序
return o1.getAge() - o2.getAge();
}
// return o1.getName() -o2.getName();//错误,减号不能运用于字符串
return o1.getName().compareTo(o2.getName());//compareTo()方法按字典顺序排序
}
});
System.out.println(arrayList);//[Student{name='aa', age=2}, Student{name='abb', age=3}, Student{name='abc', age=1}, Student{name='b', age=2}, Student{name='c', age=1}]
}
}
Collections集合工具类的常用方法就介绍到这里了,随着以后的学习了解,再逐渐更新 ~