Collection 和 Collections的区别

1、java.util.Collection

  是一个集合接口(集合类的一个顶级接口)。它提供了对集合对象进行基本操作的通用接口方法。Collection接口在Java 类库中有很多具体的实现。Collection接口的意义是为各种具体的集合提供了最大化的统一操作方式,其直接继承接口有List与Set和Queue。

2、java.util.Collections

  是一个包装类(工具类/帮助类)。它包含有各种有关集合操作的静态多态方法。此类不能实例化,就像一个工具类,用于对集合中元素进行排序、搜索以及线程安全等各种操作,服务于Java的Collection框架。

常用的有:

(1)排序 sort(Collection)

如Collections.sort(List<T> list),Collections.sort(List<T> list, Comparator<? super T> c)。

使用sort方法可以根据元素的自然顺序对指定列表按升序进行排序。列表中的所有元素都必须实现Comparable接口, 而且必须是使用指定比较器可相互比较的。

1 @SuppressWarnings("unchecked")
2 public static <T extends Comparable<? super T>> void sort(List<T> list) {
3     list.sort(null);
4 }
1 @SuppressWarnings({"unchecked", "rawtypes"})
2 public static <T> void sort(List<T> list, Comparator<? super T> c) {
3     list.sort(c);
4 }

简单示例:

1 public class Test {
2     public static void main(String[] args) {
3         List list = Arrays.asList("one two three four five".split(" "));
4         Collections.sort(list);
5         System.out.println(list);
6     }
7 }

运行结果:

[five, four, one, three, two]   //(按字母排序)

(2)混排 shuffle(Collection)

如Collections.shuffle(List<?> list)

基于随机源的输入随机排序该List,这个算法对实现一个碰运气的游戏非常有用,在生成测试案例时也十分有用。

简单示例:

1 public class Test {
2     public static void main(String[] args) {
3         List list = Arrays.asList("one two three four five".split(" "));
4         Collections.shuffle(list);
5         System.out.println(list);
6     }
7 }

运行结果:

[three, five, four, one, two]

(3)反转 reverse(Collection)

如Collections.reverse(List<?> list)
使用reverse()反转集合中元素的顺序

简单示例:

1 public class Test {
2     public static void main(String[] args) {
3         List list = Arrays.asList("one two three four five".split(" "));
4         Collections.reverse(list);
5         System.out.println(list);
6     }
7 }

运行结果:

[five, four, three, two, one]

(4)替换所有元素 fill(List list,Object o)

使用指定元素替换集合中的所有元素。

简单示例:

1 public class Test {
2     public static void main(String[] args) {
3         List list = Arrays.asList("one two three four five".split(" "));
4         Collections.fill(list, "zero");
5         System.out.println(list);
6     }
7 }

运行结果:

[zero, zero, zero, zero, zero]

(5)拷贝 copy(List list1,List list2)

将集合list2中的元素全部复制到list1中,并且覆盖相应索引的元素。目标list1至少与源list2一样长。

简单示例:

1 public class Test {
2     public static void main(String[] args) {
3         List list1 = Arrays.asList("one two three four five".split(" "));
4         List list2 = Arrays.asList("一 二 三 四 五".split(" "));
5 Collections.copy(list1, list2);
6         System.out.println(list1);
7     }
8 }

运行结果

[一, 二, 三, 四, 五]

(6)rotate(List list,int m)

根据指定的距离m循环移动列表中的元素。集合中的元素向后移m个位置,在后面被遮盖的元素循环到前面来。

简单示例:

1 public class Test {
2     public static void main(String[] args) {
3         List list1 = Arrays.asList("one two three four five".split(" "));
4         Collections.rotate(list1, 2);
5         System.out.println(list1);
6     }
7 }

运行结果:

[four, five, one, two, three]

(7)最小(大)元素 min(),max()

根据指定比较器产生的顺序,返回给定Collection的最小(大)元素。

min(Collection),min(Collection,Comparator)
max(Collection),max(Collection,Comparator)

简单示例:

 1 public class Test {
 2     public static void main(String[] args) {
 3         List list = new ArrayList();
 4         list.add(10);
 5         list.add(40);
 6         list.add(20);
 7         list.add(50);
 8         System.out.println(Collections.min(list));
 9         System.out.println(Collections.max(list));
10     }
11 }

运行结果:

10     
50

(8)indexOfSublist(List list,List sublist)

查找sublist在list中首次出现位置的索引。返回指定源列表中第一次出现指定目标列表的起始位置。

简单示例:

1 public class Test {
2     public static void main(String[] args) {
3         List list = Arrays.asList("one two three four five".split(" "));
4         List subList=Arrays.asList("three four five".split(" "));
5         System.out.println(Collections.indexOfSubList(list,subList));
6 
7     }
8 }

运行结果:

2

(9)lastIndexOfSublist(List list,List sublist)

返回指定列表中最后一次出现指定目标列表的起始位置。

(10)swap(List list,int m,int n)

交换集合中指定元素索引m,n的位置。

1 public class Test {
2     public static void main(String[] args) {
3         List list = Arrays.asList("one two three four five".split(" "));
4         Collections.swap(list, 2, 3);
5         System.out.println(list);
6     }
7 }

运行结果:

[one, two, four, three, five]

参考:

1、https://www.jianshu.com/p/0494cce4312a

2、https://www.cnblogs.com/kadaj174/p/11021730.html

 

原文地址:https://www.cnblogs.com/116970u/p/11497568.html