根本区别
1.Collection是一个集合接口。它提供了对集合对象进行基本操作的通用接口方法。
Collection接口在Java类库中有很多具体的实现。Collection接口的意义是为各种具体的集合提供最大化的统一操作方式。
以下接口实现了Collection接口:
map、set、list、vector

2.Collections是一个包装类。它包含各种有关集合操作的静态多态方法。此类不能实例化,就像一个工具类,服务于Java的Collection框架。
(1)排序(Sort)
使用sort方法可以根据元素的自然顺序对指定列表按升序进行排列。列表中所有元素都必须实现Comparable接口。
1 2 3 4 5 6 7 8 9 10 11
| public class CollectionExp { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); int array[] = {112, 111, 23, 456, 231}; for (int i = 0; i < array.length; i++) { list.add(array[i]); } Collections.sort(list); System.out.println(list); } }
|
输出结果:[23, 111, 112, 231, 456]
(2)反转(Reverse)
使用Reverse方法可以根据元素的自然顺序,对指定列表按降序进行排序。
Collections.reverse(list);
1 2 3 4 5 6 7 8 9 10 11
| public class CollectionExp { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); int array[] = {112, 111, 23, 456, 231}; for (int i = 0; i < array.length; i++) { list.add(array[i]); } Collections.reverse(list); System.out.println(list); } }
|
输出结果:[231, 456, 23, 111, 112]
(3)替换所有元素(Fill)
使用指定元素替换指定列表中的所有元素。
1 2 3 4 5 6 7 8 9 10 11
| public class CollectionExp { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); int array[] = {112, 111, 23, 456, 231}; for (int i = 0; i < array.length; i++) { list.add(array[i]); } Collections.fill(list,-1); System.out.println(list); } }
|
输出结果:[-1, -1, -1, -1, -1]
(4)拷贝(copy)
用两个参数,一个目标 List 和一个源 List, 将源的元素拷贝到目标,并覆盖它的内容。目标 List 至少与源一样长。如果它更长,则在目标 List 中的剩余元素不受影响。
Collections.copy(list,li): 前面一个参数是目标列表 ,后一个是源列表。
1 2 3 4 5 6 7 8 9 10 11
| public class CollectionExp { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); int array[] = {112, 111, 23, 456, 231}; for (int i = 0; i < array.length; i++) { list.add(array[i]); } Collections.fill(list,-1); System.out.println(list); } }
|
运行代码时报如下异常

解决办法
1 2 3 4 5 6 7 8 9 10 11 12
| public class CollectionExp { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); int array[] = {112, 111, 23, 456, 231}; for (int i = 0; i < array.length; i++) { list.add(array[i]); } List<Integer> list1 = new ArrayList<Integer>(Arrays.asList(new Integer[list.size()])); Collections.copy(list1,list); System.out.println(list1); } }
|
运行结果:[112, 111, 23, 456, 231]
(5)返回Collections中最小元素(min)
根据指定比较器产生的顺序,返回给定 collection 的最小元素。collection 中的所有元素都必须是通过指定比较器可相互比较的。
Collections.min(list)。
1 2 3 4 5 6 7 8 9 10 11
| public class CollectionExp { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); int array[] = {112, 111, 23, 456, 231}; for (int i = 0; i < array.length; i++) { list.add(array[i]); } int min = Collections.min(list); System.out.println(min); } }
|
运行结果:23
(6)返回Collections中最小元素(max)
根据指定比较器产生的顺序,返回给定 collection 的最大元素。collection 中的所有元素都必须是通过指定比较器可相互比较的。
Collections.max(list)。
1 2 3 4 5 6 7 8 9 10 11 12
| public class CollectionExp { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); int array[] = {112, 111, 23, 456, 231}; for (int i = 0; i < array.length; i++) { list.add(array[i]); } int max = Collections.max(list); System.out.println(max); } }
|
运行结果:456
————————————————
版权声明:本文为CSDN博主「行万里路,读万卷书」的原创文章
原文链接:https://blog.csdn.net/qq_40742428/article/details/100170033