Java使用Collections集合工具类

发布于:2025-05-22 ⋅ 阅读:(19) ⋅ 点赞:(0)

1、Collections 集合工具类

Java 中的 Collections 是一个非常有用的工具类,它提供了许多静态方法来操作或返回集合。这个类位于 java.util 包中,主要包含对集合进行操作的方法,比如排序、搜索、线程安全化等。

Java集合工具类的使用:

《Java使用Collections集合工具类》

《Java使用CollectionUtils集合工具类》

Java中 的 Collections 工具类提供了丰富的静态方法来操作集合,主要功能可分为以下几类:

方法 说明
一、排序操作
sort(List<T> list) 对 List 进行自然排序(元素需实现 Comparable 接口)。
sort(List<T> list, Comparator<? super T> c) 使用自定义比较器排序
reverse(List<?> list) 反转 List 元素顺序。
shuffle(List<?> list) 随机打乱 List 元素顺序。
swap(List<?> list, int i, int j) 交换指定位置元素。
二、查找与统计
max(Collection<? extends T> coll) 返回集合最大值。
min(Collection<? extends T> coll) 返回集合最小值。
frequency(Collection<?> c, Object o) 统计元素出现次数。
binarySearch(List<? extends Comparable<? super T>> list, T key) 二分查找已排序列表。
三、新增与修改
addAll(Collection<? super T> c, T... elements) 批量添加元素。
replaceAll(List<T> list, T oldVal, T newVal) 替换所有匹配元素。
fill(List<? super T> list, T obj) 用指定元素填充列表。
copy(List<? super T> dest, List<? extends T> src) 列表复制。
四、不可变集合
unmodifiableCollection(Collection<? extends T> c) 返回不可修改集合视图。
unmodifiableList(List<? extends T> list) 返回不可修改 List 视图。
unmodifiableSet(Set<? extends T> s) 返回不可修改 Set 视图。
unmodifiableMap(Map<? extends K,? extends V> m) 返回不可修改 Map 视图。
五、同步控制
synchronizedCollection(Collection<T> c) 返回线程安全集合。
synchronizedList(List<T> list) 返回线程安全 List。
synchronizedSet(Set<T> s) 返回线程安全 Set。
synchronizedMap(Map<K,V> m) 返回线程安全 Map。
六、特殊集合
emptyList()、emptySet()、emptyMap() 返回空集合不可变实例。
singletonList(T o)、singletonSet(T o)、singletonMap(K k, V v) 返回单元素不可变集合。
nCopies(int n, T o) 返回包含 n 个相同元素的不可变 List。
checkedCollection(Collection<E> c, Class<E> type) 返回类型安全集合视图。
七、其他方法
disjoint(Collection<?> c1, Collection<?> c2) 判断两集合是否无交集。
indexOfSubList(List<?> source, List<?> target) 返回子列表首次出现位置。
lastIndexOfSubList(List<?> source, List<?> target) 返回子列表最后出现位置。
rotate(List<?> list, int distance) 旋转列表元素。

2、 Collections 方法的使用

Collections 类的方法都是静态的,使用时直接通过类名调用,如 Collections.max(list)。这些方法大大简化了集合操作,特别是对 List 的常见处理。 

【示例】使用 Collections 工具类提供的方法,模拟抽奖活动。

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 抽奖活动
 * @author pan_junbiao
 **/
public class CollectionsTest
{
    public static void main(String[] args)
    {
        // 创建集合列表(奖池)
        List<Integer> jackpot = new ArrayList<>();

        // 奖池添加数据
        Collections.addAll(jackpot, 25, 100, 5, 60, 888, 90);
        System.out.println("奖池中的金额:" + jackpot);

        // 统计
        Integer maxPrize = Collections.max(jackpot);
        Integer minPrize = Collections.min(jackpot);
        System.out.println("奖池中的最大金额:" + maxPrize);
        System.out.println("奖池中的最小金额:" + minPrize);

        // 随机打乱奖池顺序
        Collections.shuffle(jackpot);
        System.out.println("抽奖结果:" + jackpot);
    }
}

执行结果:

 3、Collections 的排序方法

Collections 类的 sort() 方法、reverse()方法说明:

Collections.sort()方法:自然排序‌:对 List 集合中的元素进行升序排列,要求元素必须实现 Comparable 接口(如String、Integer等已默认实现)。

Collections.reverse()方法:反转 List 集合中元素的顺序,不依赖元素是否可比较。

Collections 类提供了两种 sort() 排序方法,分别如下:

(1)sort() 排序方法一:

public static <T extends Comparable<? super T>> void sort(List<T> list) 
{
    list.sort(null);
}

(2)sort() 排序方法二:

public static <T> void sort(List<T> list, Comparator<? super T> c) {
    list.sort(c);
}

该方法中指定比较方式 Comparator<? super T> c,即 c 必须实现 Comparator<? super T> 接口,重写 compareTo() 方法指定比较项目。比较项目在类外指定,比较灵活。

【示例】使用 Collections 类的 sort() 方法、reverse()方法,实现对 List 列表进行排序与倒序。

/**
 * 使用 Collections 类的 sort() 方法、reverse()方法,
 * 实现对 List 列表进行排序与倒序。
 * @author pan_junbiao
 */
@Test
public void testCollectionsSort()
{
    //创建列表(故意打乱顺序)
    List<String> userList = new ArrayList<>();
    userList.add("pan_junbiao的博客_03");
    userList.add("pan_junbiao的博客_01");
    userList.add("pan_junbiao的博客_04");
    userList.add("pan_junbiao的博客_02");
 
    //执行排序方法(正序排序)
    Collections.sort(userList);
    System.out.println("正序排序:");
    userList.stream().forEach(System.out::println);
 
    //执行排序方法(倒序排序)
    Collections.reverse(userList);
    System.out.println("\n倒序排序:");
    userList.stream().forEach(System.out::println);
}

执行结果:


网站公告

今日签到

点亮在社区的每一天
去签到