Java 提供了原生的 java.util.Collections 工具类,里面包含了大量的静态方法服务于 Collection 的各种操作,特此记录和大家一起学习!
目录- 1.返回类型安全的容器
- 2.获取容器内最大/最小的元素
- 3.获取LIst片段第一次/最后一次出现的位置
- 4.批量替换
- 5.List倒序
- 6.打乱元素顺序
- 7.自定义排序
- 8.复制元素
- 9.交换元素位置
- 10.填充元素
- 11.快速构建List
- 12.判断元素是否有交集
- 13.统计元素个数
- 14.返回空容器
- 返回受到类型安全检查的容器
Collections.checkedList(List
list, Class type)
list 为受检查的容器,type 为被检查的 class 类型
checkedList() 可以返回对容器进行安全检查的视图,当我们试图往容器中插入不正确的类型时,会抛出类型转换异常:ClassCastException ;
public static void main(String[] args) {
Integer[] integers = {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6};
ArrayList arrayList = new ArrayList<>(Arrays.asList(integers));
List list = arrayList;
list.add("a");
System.out.println(list);
List typeSafeList = Collections.checkedList(arrayList, Integer.class);
list = typeSafeList;
list.add("b");
}
[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, a] Exception in thread "main" java.lang.ClassCastException: Attempt to insert class java.lang.String element into collection with element type class java.lang.Integer at java.util.Collections$CheckedCollection.typeCheck(Collections.java:3037) at java.util.Collections$CheckedCollection.add(Collections.java:3080) at mtn.baymax.charpter17.CollectionsExpand.main(CollectionsExpand.java:20)
相同的方法类似还有:
- 返回安全类型检查的 Colleciton
checkedCollection(Collection
c, Class type)
- 返回安全类型检查的 Map
checkedMap(Map
m, Class keyType, Class valueType)
- 返回安全类型检查的 Set
checkedSet(Set
s, Class type)
- 返回安全类型检查的 SortedMap
checkedSortedMap(SortedMap
m, Class keyType, Class valueType)
- 返回安全类型检查的 SortedSet
2.获取容器内最大/最小的元素checkedSortedSet(SortedSet
s, Class type)
- 返回容器内最大的元素
max(Collection extends T> coll)
- 返回容器内最小的元素
min(Collection extends T> coll)
public static void main(String[] args) {
Integer[] integers = {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6};
ArrayList arrayList = new ArrayList<>(Arrays.asList(integers));
Integer max = Collections.max(arrayList);
System.out.println(max);
Integer min = Collections.min(arrayList);
System.out.println(min);
}
6 1
我们也可以通过实现 Comparator 接口来自定义比较规则:
max(Collection extends T> coll, Comparator super T> comp)
min(Collection extends T> coll, Comparator super T> comp)
IntegerComparator 为自定义的比较规则,认为最小的即为“最大值”,故 Collections.max() 返回 1;
public static void main(String[] args) {
Integer[] integers = {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6};
ArrayList arrayList = new ArrayList<>(Arrays.asList(integers));
class IntegerComparator implements Comparator {
@Override
public int compare(Integer i1, Integer i2) {
if (i1 > i2) return -1;
if (i1 < i2) return 1;
return 0;
}
}
Integer max = Collections.max(arrayList,new IntegerComparator());
System.out.println(max);
}
13.获取LIst片段第一次/最后一次出现的位置
- 获取 List 片段第一次出现的位置
indexOfSubList(List> source, List> target) ;
source 为源容器,target 为对比的片段
- 获取 List 片段最后一次出现的位置
lastIndexOfSubList(List> source, List> target) ;
source 为源容器,target 为对比的片段
public static void main(String[] args) {
Integer[] integers = {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6};
ArrayList a1 = new ArrayList<>(Arrays.asList(integers));
Integer[] ints = {4, 5, 6};
ArrayList a2 = new ArrayList<>(Arrays.asList(ints));
System.out.println(Collections.indexOfSubList(a1, a2));
System.out.println(Collections.lastIndexOfSubList(a1, a2));
}
3 154.批量替换
- 批量替换某个值
replaceAll(List list, T oldVal, T newVal)
list 为执行容器,oldVal 为旧值,newVal 为新值
public static void main(String[] args) {
Integer[] integers = {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6};
ArrayList arrayList = new ArrayList<>(Arrays.asList(integers));
Collections.replaceAll(arrayList,1,0);
System.out.println(arrayList);
}
[0, 2, 3, 4, 5, 6, 0, 2, 3, 4, 5, 6, 0, 2, 3, 4, 5, 6]5.List倒序
- List 倒序排列
reverse(List> list)
public static void main(String[] args) {
Integer[] integers = {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6};
ArrayList arrayList = new ArrayList<>(Arrays.asList(integers));
Collections.reverse(arrayList);
System.out.println(arrayList);
}
[6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1]
- 返回逆转容器内元素自然排序规则的 Comparator
reverseOrder()
- 将传入的 Comparator 排序规则逆转
reverseOrder(Comparator cmp)
public static void main(String[] args) {
Integer[] integers = {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6};
ArrayList arrayList = new ArrayList<>(Arrays.asList(integers));
Comparator
[6, 6, 6, 5, 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 2, 1, 1, 1] [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6]6.打乱元素顺序
- 打乱容器内元素顺序
shuffle(List> list)
- 打乱容器内元素顺序,并指定随机来源(可确保每次运行时,获得的随机数是一致的)
shuffle(List> list, Random rnd)
public static void main(String[] args) {
Integer[] integers = {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6};
ArrayList a1 = new ArrayList<>(Arrays.asList(integers));
Collections.shuffle(a1, new Random(47));
System.out.println(a1);
Collections.shuffle(a1);
System.out.println(a1);
ArrayList a2 = new ArrayList<>(Arrays.asList(integers));
Collections.shuffle(a2, new Random(47));
System.out.println(a2);
Collections.shuffle(a2);
System.out.println(a2);
}
[1, 2, 2, 1, 4, 4, 2, 4, 5, 6, 3, 5, 1, 6, 6, 5, 3, 3] [2, 1, 4, 1, 5, 6, 5, 2, 1, 4, 6, 3, 5, 3, 3, 2, 4, 6] [1, 2, 2, 1, 4, 4, 2, 4, 5, 6, 3, 5, 1, 6, 6, 5, 3, 3] [2, 6, 4, 6, 1, 1, 4, 5, 3, 3, 2, 4, 1, 2, 5, 5, 3, 6]7.自定义排序
- 自然升序
sort(List list)
- 自定义排序
sort(List list, Comparator super T> c)
list 为排序容器,Comparator 为制定排序规则的实现类
public static void main(String[] args) {
Integer[] integers = {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6};
ArrayList arrayList = new ArrayList<>(Arrays.asList(integers));
Collections.sort(arrayList);
System.out.println(arrayList);
//降序排列的Comparator实现
class IntegerComparator implements Comparator {
@Override
public int compare(Integer i1, Integer i2) {
if (i1 > i2) return -1;
if (i1 < i2) return 1;
return 0;
}
}
Collections.sort(arrayList, new IntegerComparator());
System.out.println(arrayList);
}
[1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6] [6, 6, 6, 5, 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 2, 1, 1, 1]8.复制元素
- 将一个 List 的元素复制到另一个 List,会替换原有容器内对应位置的元素
copy(List super T> dest, List extends T> src)
dest 为目标容器,src 为复制源
public static void main(String[] args) {
Integer[] ints1 = {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6};
ArrayList a1 = new ArrayList<>(Arrays.asList(ints1));
Integer[] ints2 = {11, 12, 13, 14, 15, 16};
ArrayList a2 = new ArrayList<>(Arrays.asList(ints2));
Collections.copy(a1,a2);
System.out.println(a1);
}
[11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]9.交换元素位置
- 交换对应下标元素的位置,通常比自己实现的方法效率高
swap(List> list, int i, int j)
list 为执行容器,i 和 j 为两个位置对应的下标
public static void main(String[] args) {
Integer[] integers = {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6};
ArrayList arrayList = new ArrayList<>(Arrays.asList(integers));
Collections.swap(arrayList, 0, 5);
System.out.println(arrayList);
}
[6, 2, 3, 4, 5, 1, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]10.填充元素
- 使用指定元素填充容器内所有位置的元素
fill(List super T> list, T obj)
list 为执行容器, obj 为填充对象
public static void main(String[] args) {
Integer[] integers = {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6};
ArrayList arrayList = new ArrayList<>(Arrays.asList(integers));
Collections.fill(arrayList, 0);
System.out.println(arrayList);
}
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]11.快速构建List
- 返回指定长度、由 T 构成的 List
List nCopies(int n, T o)
n 为容器长度,o 为填充对象
public static void main(String[] args) {
List integers = Collections.nCopies(10, 12);
System.out.println(integers);
List doubles = Collections.nCopies(10, 2.4);
System.out.println(doubles);
List strings = Collections.nCopies(10, "A");
System.out.println(strings);
}
[12, 12, 12, 12, 12, 12, 12, 12, 12, 12] [2.4, 2.4, 2.4, 2.4, 2.4, 2.4, 2.4, 2.4, 2.4, 2.4] [A, A, A, A, A, A, A, A, A, A]12.判断元素是否有交集
- 判断两个容器是否有交集,当没有任何相同的元素时返回 true ,否则返回 false
disjoint(Collection> c1, Collection> c2)
public static void main(String[] args) {
Integer[] ints1 = {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6};
ArrayList a1 = new ArrayList<>(Arrays.asList(ints1));
Integer[] ints2 = {1, 2, 3, 4, 5, 6};
ArrayList a2 = new ArrayList<>(Arrays.asList(ints2));
System.out.println(Collections.disjoint(a1, a2));
Integer[] ints3 = {7, 8, 9};
ArrayList a3 = new ArrayList<>(Arrays.asList(ints3));
System.out.println(Collections.disjoint(a1, a3));
}
false true13.统计元素个数
- 统计容器内相同元素的个数
frequency(Collection> c, Object o)
c 为执行容器,o 为需统计的对象
public static void main(String[] args) {
Integer[] integers = {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6};
ArrayList arrayList = new ArrayList<>(Arrays.asList(integers));
System.out.println(Collections.frequency(arrayList, 5));
}
314.返回空容器
- 返回空 List
List emptyList()
- 返回空 List
List emptyList()
- 返回空 List
List emptyList()
返回值支持指定泛型,当试图往容器添加元素时,会抛出不支持此操作的异常: UnsupportedOperationException
public static void main(String[] args) {
List list = Collections.emptyList();
Map map = Collections.emptyMap();
Set set = Collections.emptySet();
list.add("a");
}
Exception in thread "main" java.lang.UnsupportedOperationException at java.util.AbstractList.add(AbstractList.java:148) at java.util.AbstractList.add(AbstractList.java:108) at mtn.baymax.charpter17.CollectionsExpand.main(CollectionsExpand.java:16)
本次分享至此结束,希望本文对你有所帮助,若能点亮下方的点赞按钮,在下感激不尽,谢谢您的【精神支持】。
若有任何疑问,也欢迎与我交流,若存在不足之处,也欢迎各位指正!



