Collections 类的常用方法
-
static<T extends Comparable<? super T>>min(Collection elements)
-
static<T extends Comparable<? super T>>max(Collection elements)
-
static min(Collection elements,Comparator<? super T>) c)
-
static max(Collection elements,Comparator<? super T>) c)
返回集合当中最小或者最大的元素代码示例:
//创建集合 ArrayList<Integer>ar1=new ArrayList<>(List.of(1, 3, 4, 6, 7, 9, 2)); //打印集合最大值最小值 System.out.println("集合当中最小值为: "+Collections.min(ar1)); System.out.println("集合当中最大值为: "+Collections.max(ar1)); //创建集合 ArrayList<Student>ar2=new ArrayList<>(List.of(new Student("li","2019020810",20),(new Student("zhang","2017020810",21)))); //获取集合当中的最大值最小值(按照年龄排序) //用的Comparator接口的静态方法 Student min1=Collections.min(ar2, Comparator.comparing(Student::getAge)); //用的Lamda表达式 Student min2=Collections.max(ar2, ((t0, t1 ) -> {return t0.getAge()-t1.getAge(); })); //用的匿名内部类 Student min3=Collections.min(ar2, new Comparator<Student>() { @Override public int compare(Student student, Student t1) { return student.getAge()-t1.getAge(); } }); //按照年龄排序 System.out.println("集合中最小值为"+min1); System.out.println("集合中最小值为"+min2);
-
static void copy(List<? super T>to,List from)
将元列表中的所有元素复制到目标列表的相应位置上。目标列表的长度至少要与原列表一样
代码示例:
//创建集合
ArrayList<Integer>ar1=new ArrayList<>(List.of(1, 3, 4, 6, 7, 9, 2));
ArrayList<Integer>ar2=new ArrayList(Arrays.asList((new Integer[ar1.size()])));
//把ar1的元素复制到ar2对应位置也一样
//前提是第一个集合的大小至少要大于等于第二个集合
Collections.copy(ar2,ar1);
//打印第二个集合ar2
ar2.forEach(a-> System.out.println(a));
- static void fill(List<? super T>l,T value)
将列表中所有的位置设置成相同的值
//创建集合
ArrayList ar1=new ArrayList(Arrays.asList(new String[3]));
//填充3个字符串star到ar1集合中
Collections.fill(ar1,"star");
//打印集合ar1
System.out.println(ar1);
- static boolean addAll(Collection<? super T>c,T…values)
将所有的值添加到给定的集合中。如果集合改变了则返回true
//创建集合
ArrayList ar1=new ArrayList(Arrays.asList());
//将所有的值添加到给定的集合中
Collections.addAll(ar1,new Student("li","1117",20),new Student("zhang","1028",21));
//打印集合
System.out.println(ar1);
- static boolean replaceAll(Listl,T oldValue,T newValue)
用newValue 值替换所有值为oldValue的元素
//创建集合
ArrayList ar1=new ArrayList(Arrays.asList());
//将所有的值添加到给定的集合中
Collections.addAll(ar1,10,20,30,50);
//把50替换成40
Collections.replaceAll(ar1, 50, 40);
//打印集合
System.out.println(ar1);
- static int indexOfSubList(List<?>l,List<?>s)
- static int lastIndexOfSubList(List<?>l,List<?>s)
返回l中的第一个或者最后一个等于s的子列表的索引。如果l中不存在等于s的子列表则返回-1.例如,l 为 {s,t,a,r},s为{t,a,r},两个方法都将返回索引1
//创建集合
ArrayList ar1=new ArrayList();
//将所有的值添加到给定的集合中
Collections.addAll(ar1,10,20,30,40,50,60,70,80,90,100,40,50,60,70,80);
ArrayList ar2 = new ArrayList(List.of(40,50,60,70));
//子列表在主列表中第一次出现的位置
System.out.println(Collections.indexOfSubList(ar1, ar2));
//子列表在主列表中最后一次出现的位置
System.out.println(Collections.lastIndexOfSubList(ar1, ar2));
//打印集合
System.out.println(ar1);
- static void swap(List<?>l,int i,int j)
交还给定的飘移位置的两个元素
ArrayList ar1 = new ArrayList(List.of(20,50,40,30,60,70));
//交换集合ar1索引1 和3的值
Collections.swap(ar1,1,3);
//打印集合
System.out.println(ar1);
- static void reverse(List<?>l)
逆置列表中的元素的顺序,例如,逆置列表【t,a,r】后将得到列表【r,a,t】。这个方法的时间复杂度为O(n),n为列表长度
//创建集合
ArrayList ar1 = new ArrayList(List.of(20,50,40,30,60,70));
//集合元素逆序
Collections.reverse(ar1);
//打印集合
System.out.println(ar1);
}
- static int frequency(Collection<?>c,Object o)
返回c 中与对象 o相等的元素个数
//创建集合
ArrayList ar1 = new ArrayList(List.of(20,50,40,30,60,70,20));
//返回参数二在第一个参数集合中出现过的次数
System.out.println(Collections.frequency(ar1, 20));
//打印集合
System.out.println(ar1);
- boolean disjoint(Collection<?>c1,Collection<?>c2)
如果两个集合没有共同的元素,则返回true
//创建集合
ArrayList ar1 = new ArrayList(List.of(20, 50, 40, 30, 60, 70, 20));
ArrayList ar2 = new ArrayList(List.of(1, 2, 3, 4, 5, 6));
//如果两个集合没有共同的元素,则返回true
System.out.println(Collections.disjoint(ar1, ar2));
- static <T extends Comparable<? super T>>void sort(List ele,emts)
使用稳定的排序算法对列表中的元素进行排序
//创建集合
ArrayList ar1 = new ArrayList(List.of(20, 50, 40, 30, 60, 70, 20));
ArrayList<Student> ar2 = new ArrayList();
Student[] students={
new Student("li","1117",20),
new Student("zhang","1028",21)
};
//把数组元素全部传给ar2 也可以用List.of()
Collections.addAll(ar2, students);
//默认的升序
Collections.sort(ar1);
//降序
Collections.sort(ar1,Collections.reverseOrder());
//按照年龄排序
Collections.sort(ar2,Comparator.comparing(Student::getAge));
//按照姓名的长度排序
Collections.sort(ar2,(a,b)->{return a.getSname().length()-b.getSname().length();});
Collections.sort(ar2, new Comparator<Student>() {
@Override
public int compare(Student s1, Student t1) {
return s1.getSname().length()-t1.getSname().length();
}
});
- static void shuffle(List<?> elements)
随机地打乱列表中的元素的顺序。
//创建集合
ArrayList ar1 = new ArrayList(List.of(20, 50, 40, 30, 60, 70, 20));
//打乱集合顺序
Collections.shuffle(ar1);
- static Collection synchronizedList(List l)
返回一个由指定集合支持的同步(线程安全)集。
//创建集合
ArrayList ar1 = new ArrayList(List.of(20, 50, 40, 30, 60, 70, 20));
//使集合变成线程同步安全集
Collections.synchronizedList(ar1);
- static Collection unmodifiableCollection(Collection<?
extends T> c)
返回指定集合的一个不可修改的视图。不可修改的视图并不是集合本身不可以更改,仍然可以通过集合的(原始引用)对集合进行修改,并且可以对集合的元素调用更改器方法