一、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) | 用指定的元素替换指定列表中的所有元素。 |
List<String> list = new ArrayList<>();
Collections.addAll(list, "Apple", "Banana", "Cherry");
System.out.println("List after addAll: " + list);
Collections.shuffle(list);
System.out.println("List after shuffle: " + list);
Collections.sort(list);
System.out.println("List after sort: " + list);
Collections.sort(list, Comparator.reverseOrder());
System.out.println("List after reverse sort: " + list);
int index = Collections.binarySearch(list, "Banana");
System.out.println("Index of 'Banana': " + index);
List<String> destList = new ArrayList<>(Arrays.asList(new String[list.size()]));
Collections.copy(destList, list);
System.out.println("Destination list after copy: " + destList);
Collections.swap(list, 0, 1);
System.out.println("List after swap: " + list);
String max = Collections.max(list);
System.out.println("Max element: " + max);
String min = Collections.min(list);
System.out.println("Min element: " + min);
Collections.fill(list, "Kiwi");
System.out.println("List after fill: " + list);