JAVA小知识24:集合工具类Collections

一、Collections简介

Collections 是 Java 中的一个实用类,位于 java.util 包中,提供了许多静态方法,用于操作或返回集合的各种操作。它包含的这些方法大大简化了对集合的常见操作,提供了排序、搜索、随机化、同步化等功能。

二、成员方法

方法说明
Collections.addAll(Collection<? super T> c, T... elements)将所有指定元素添加到指定集合中。
Collections.sort(List<T> list)根据元素的自然顺序对指定列表按升序进行排序。
Collections.sort(List<T> list, Comparator<? super T> c)根据指定比较器产生的顺序对列表进行排序。
Collections.shuffle(List<?> list)使用默认随机源随机排列指定的列表。
Collections.shuffle(List<?> list, Random rnd)使用指定的随机源随机排列指定的列表。
Collections.reverse(List<?> list)反转指定列表中元素的顺序。
Collections.swap(List<?> list, int i, int j)交换指定列表中指定位置的元素。
Collections.max(Collection<? extends T> coll)根据元素的自然顺序返回给定集合中的最大元素。
Collections.max(Collection<? extends T> coll, Comparator<? super T> comp)根据指定比较器产生的顺序,返回给定集合中的最大元素。
Collections.min(Collection<? extends T> coll)根据元素的自然顺序返回给定集合中的最小元素。
Collections.min(Collection<? extends T> coll, Comparator<? super T> comp)根据指定比较器产生的顺序,返回给定集合中的最小元素。
Collections.frequency(Collection<?> c, Object o)返回指定集合中等于指定对象的元素的次数。
Collections.disjoint(Collection<?> c1, Collection<?> c2)如果两个指定集合没有相同的元素,则返回 true
Collections.copy(List<? super T> dest, List<? extends T> src)将源列表中的所有元素复制到目标列表中。
Collections.replaceAll(List<T> list, T oldVal, T newVal)使用另一个值替换列表中所有出现的指定值。
Collections.fill(List<? super T> list, T obj)用指定的元素替换指定列表中的所有元素。
Collections.binarySearch(List<? extends Comparable<? super T>> list, T key)使用二分搜索法搜索指定列表,以获得指定对象。
Collections.binarySearch(List<? extends T> list, T key, Comparator<? super T> c)使用指定的比较器对指定列表进行二分搜索,以获得指定对象。
Collections.unmodifiableCollection(Collection<? extends T> c)返回指定集合的不可修改视图。
Collections.unmodifiableList(List<? extends T> list)返回指定列表的不可修改视图。
Collections.unmodifiableMap(Map<? extends K, ? extends V> m)返回指定映射的不可修改视图。
Collections.unmodifiableSet(Set<? extends T> s)返回指定集合的不可修改视图。
Collections.synchronizedCollection(Collection<T> c)返回由指定集合支持的同步(线程安全)集合。
Collections.synchronizedList(List<T> list)返回由指定列表支持的同步(线程安全)列表。
Collections.synchronizedMap(Map<K, V> m)返回由指定映射支持的同步(线程安全)映射。
Collections.synchronizedSet(Set<T> s)返回由指定集合支持的同步(线程安全)集合。
Collections.singletonList(T o)返回一个只包含指定对象的不可变列表。
Collections.singleton(T o)返回一个只包含指定对象的不可变集合。
Collections.singletonMap(K key, V value)返回一个只包含指定键值映射关系的不可变映射。
Collections.emptyList()返回一个空的不可变列表。
Collections.emptySet()返回一个空的不可变集合。
Collections.emptyMap()返回一个空的不可变映射。
Collections.nCopies(int n, T o)返回由指定对象的 n 个副本组成的不可变列表。
Collections.reverseOrder()返回一个比较器,它强制执行自然顺序的反转。
Collections.reverseOrder(Comparator<T> cmp)返回一个比较器,它强制执行指定比较器的顺序的反转。

三、重要成员方法详解

方法说明
Collections.addAll(Collection<? super T> c, T... elements)将所有指定元素添加到指定集合中。
Collections.shuffle(List<?> list)使用默认随机源随机排列指定的列表。
Collections.sort(List<T> list)根据元素的自然顺序对指定列表按升序进行排序。
Collections.sort(List<T> list, Comparator<? super T> c)根据指定比较器产生的顺序对列表进行排序。
Collections.binarySearch(List<? extends Comparable<? super T>> list, T key)使用二分搜索法搜索指定列表,以获得指定对象。
Collections.copy(List<? super T> dest, List<? extends T> src)将源列表中的所有元素复制到目标列表中。
Collections.swap(List<?> list, int i, int j)交换指定列表中指定位置的元素。
Collections.max(Collection<? extends T> coll)根据元素的自然顺序返回给定集合中的最大元素。
Collections.min(Collection<? extends T> coll)根据元素的自然顺序返回给定集合中的最小元素。
Collections.fill(List<? super T> list, T obj)用指定的元素替换指定列表中的所有元素。
// `Collections.addAll(Collection<? super T> c, T... elements)`
// 将所有指定元素添加到指定集合中。
List<String> list = new ArrayList<>();
Collections.addAll(list, "Apple", "Banana", "Cherry");
System.out.println("List after addAll: " + list); // [Apple, Banana, Cherry]

// `Collections.shuffle(List<?> list)`
// 使用默认随机源随机排列指定的列表。
Collections.shuffle(list);
System.out.println("List after shuffle: " + list);

// `Collections.sort(List<T> list)`
// 根据元素的自然顺序对指定列表按升序进行排序。
Collections.sort(list);
System.out.println("List after sort: " + list); // [Apple, Banana, Cherry]

// `Collections.sort(List<T> list, Comparator<? super T> c)`
// 根据指定比较器产生的顺序对列表进行排序。
Collections.sort(list, Comparator.reverseOrder());
System.out.println("List after reverse sort: " + list); // [Cherry, Banana, Apple]

// `Collections.binarySearch(List<? extends Comparable<? super T>> list, T key)`
// 使用二分搜索法搜索指定列表,以获得指定对象。
int index = Collections.binarySearch(list, "Banana");
System.out.println("Index of 'Banana': " + index);

// `Collections.copy(List<? super T> dest, List<? extends T> src)`
// 将源列表中的所有元素复制到目标列表中。
List<String> destList = new ArrayList<>(Arrays.asList(new String[list.size()]));
Collections.copy(destList, list);
System.out.println("Destination list after copy: " + destList); // [Cherry, Banana, Apple]

// `Collections.swap(List<?> list, int i, int j)`
// 交换指定列表中指定位置的元素。
Collections.swap(list, 0, 1);
System.out.println("List after swap: " + list); // [Banana, Cherry, Apple]

// `Collections.max(Collection<? extends T> coll)`
// 根据元素的自然顺序返回给定集合中的最大元素。
String max = Collections.max(list);
System.out.println("Max element: " + max); // Cherry

// `Collections.min(Collection<? extends T> coll)`
// 根据元素的自然顺序返回给定集合中的最小元素。
String min = Collections.min(list);
System.out.println("Min element: " + min); // Apple

// `Collections.fill(List<? super T> list, T obj)`
// 用指定的元素替换指定列表中的所有元素。
Collections.fill(list, "Kiwi");
System.out.println("List after fill: " + list); // [Kiwi, Kiwi, Kiwi]
                                                                                                                                                            
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值