全部学科
NodeJS全栈
nodejs
Python全栈
python
小程序首页
📅 2026-05-10 7 分钟 ✍️ juanwangdev

集合工具类 Collections

Collections 位于 java.util 包,提供操作集合的静态工具方法。

排序操作

Java
List<Integer> list = new ArrayList<>(Arrays.asList(3, 1, 2, 5, 4));

// 自然排序
Collections.sort(list);         // [1, 2, 3, 4, 5]

// 降序排序
Collections.sort(list, Comparator.reverseOrder());  // [5, 4, 3, 2, 1]

// 自定义排序
Collections.sort(list, (a, b) -> b - a);  // 降序

// Java 8+ 简化写法
list.sort(Comparator.naturalOrder());    // 升序
list.sort(Comparator.reverseOrder());    // 降序

查找操作

Java
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

// 二分查找(需先排序)
int index = Collections.binarySearch(list, 3);  // 2

// 查找最大/最小
Integer max = Collections.max(list);           // 5
Integer min = Collections.min(list);           // 1

// 自定义比较器查找
Integer max = Collections.max(list, Comparator.reverseOrder());  // 1

binarySearch 要求:集合必须已排序,否则结果不确定。

反转与打乱

Java
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

// 反转
Collections.reverse(list);      // [5, 4, 3, 2, 1]

// 打乱(随机排列)
Collections.shuffle(list);      // 随机顺序
Collections.shuffle(list, new Random(42));  // 指定随机种子

填充与复制

Java
List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C"));

// 填充所有元素
Collections.fill(list, "X");    // [X, X, X]

// 复制到另一个列表
List<String> dest = new ArrayList<>(Arrays.asList("", "", ""));
Collections.copy(dest, list);   // dest = [A, B, C]

copy 要求:目标列表长度必须 ≥ 源列表长度。

交换与旋转

Java
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

// 交换指定位置
Collections.swap(list, 0, 2);   // [3, 2, 1, 4, 5]

// 旋转(循环移动)
Collections.rotate(list, 2);    // 右移 2 位:[4, 5, 1, 2, 3]
Collections.rotate(list, -2);   // 左移 2 位:[3, 4, 5, 1, 2]

频率与查找

Java
List<String> list = Arrays.asList("A", "B", "A", "C", "A");

// 元素出现频率
int freq = Collections.frequency(list, "A");  // 3

// 查找子列表位置
List<String> subList = Arrays.asList("B", "A");
int index = Collections.indexOfSubList(list, subList);  // 1

同步包装

Java
// 包装为线程安全的集合
List<String> syncList = Collections.synchronizedList(new ArrayList<>());
Set<String> syncSet = Collections.synchronizedSet(new HashSet<>());
Map<String, String> syncMap = Collections.synchronizedMap(new HashMap<>());

注意:同步包装类遍历时需手动加锁:

Java
List<String> syncList = Collections.synchronizedList(new ArrayList<>());
synchronized (syncList) {
    for (String s : syncList) {
        // 遍历操作
    }
}

不可变集合

Java
// 创建不可变集合(只读)
List<String> unmodifiableList = Collections.unmodifiableList(list);
Set<String> unmodifiableSet = Collections.unmodifiableSet(set);
Map<String, String> unmodifiableMap = Collections.unmodifiableMap(map);

// 尝试修改会抛 UnsupportedOperationException
unmodifiableList.add("X");  // 抛异常

Java 9+ 简化List.of(), Set.of(), Map.of() 创建不可变集合。

空集合

Java
// 返回不可变的空集合(避免返回 null)
List<String> emptyList = Collections.emptyList();
Set<String> emptySet = Collections.emptySet();
Map<String, String> emptyMap = Collections.emptyMap();

适用场景:方法返回空集合时,返回 emptyList() 代替 null,避免 NPE。

单元素集合

Java
// 创建只包含一个元素的不可变集合
List<String> singletonList = Collections.singletonList("A");
Set<String> singletonSet = Collections.singleton("A");
Map<String, String> singletonMap = Collections.singletonMap("key", "value");

要点总结

  • Collections.sort():排序,可指定比较器
  • Collections.binarySearch():二分查找(需先排序)
  • Collections.reverse():反转,Collections.shuffle():打乱
  • Collections.max/min():查找最大最小值
  • Collections.synchronizedXxx():同步包装
  • Collections.unmodifiableXxx():不可变集合
  • Collections.emptyList():返回空集合,避免 null
  • Java 9+ 可用 List.of()/Set.of()/Map.of() 创建不可变集合

📝 发现内容有误?点击此处直接编辑

← 上一篇 泛型与集合
下一篇 → 集合性能分析
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库