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

Collections

Collections是Java标准库的集合工具类,提供集合的排序、查找、操作等功能。

概述

所在包

java.util.Collections

核心特点

  • 静态方法,无需创建对象
  • 操作List、Set、Map等集合
  • 提供排序、查找、不可变集合等功能

排序方法

sort排序

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

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

// 自定义排序(降序)
Collections.sort(list, Collections.reverseOrder());
// [8, 5, 2, 1]

// Comparator自定义排序
Collections.sort(list, (a, b) -> b - a);
// [8, 5, 2, 1]

reverse反转

Java
List<Integer> list = Arrays.asList(1, 2, 3);
Collections.reverse(list);
// [3, 2, 1]

查找方法

binarySearch二分查找

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

// 必须先排序
int index = Collections.binarySearch(list, 5);
// 2(找到返回索引)

int index = Collections.binarySearch(list, 4);
// -2(找不到返回-(插入点)-1)

注意:binarySearch要求List已排序,否则结果不确定。

max/min最大最小值

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

int max = Collections.max(list);  // 8
int min = Collections.min(list);  // 1

// 自定义比较器
int max = Collections.max(list, Comparator.reverseOrder());  // 1(反转后最小)

填充与替换

fill填充

Java
List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));
Collections.fill(list, "x");
// ["x", "x", "x"]

replaceAll替换

Java
List<String> list = Arrays.asList("a", "b", "a", "c");
Collections.replaceAll(list, "a", "x");
// ["x", "b", "x", "c"]

元素顺序操作

shuffle随机打乱

Java
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
Collections.shuffle(list);
// 随机顺序,如 [3, 1, 5, 2, 4]

swap交换元素

Java
List<Integer> list = Arrays.asList(1, 2, 3);
Collections.swap(list, 0, 2);
// [3, 2, 1](交换索引0和2)

rotate旋转

Java
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
Collections.rotate(list, 2);
// [4, 5, 1, 2, 3](向右旋转2位)

不可变集合

创建不可变集合

Java
// 不可变List
List<String> unmodList = Collections.unmodifiableList(
    new ArrayList<>(Arrays.asList("a", "b"))
);

// 不可变Set
Set<String> unmodSet = Collections.unmodifiableSet(
    new HashSet<>(Arrays.asList("a", "b"))
);

// 不可变Map
Map<String, Integer> unmodMap = Collections.unmodifiableMap(
    new HashMap<>()
);

不可变集合不能添加、删除、修改元素,会抛出UnsupportedOperationException。

空集合与单元素集合

空集合

Java
List<String> emptyList = Collections.emptyList();
Set<String> emptySet = Collections.emptySet();
Map<String, String> emptyMap = Collections.emptyMap();

// 特点:不可变、节省内存、避免null

单元素集合

Java
List<String> singletonList = Collections.singletonList("a");
Set<String> singletonSet = Collections.singleton("a");
// 不可变,只有一个元素

同步集合

创建线程安全集合

Java
List<String> syncList = Collections.synchronizedList(new ArrayList<>());
Set<String> syncSet = Collections.synchronizedSet(new HashSet<>());
Map<String, String> syncMap = Collections.synchronizedMap(new HashMap<>());

同步集合内部使用synchronized保证线程安全,但性能较低,推荐使用并发集合(如CopyOnWriteArrayList)。

频率与索引

frequency统计频率

Java
List<String> list = Arrays.asList("a", "b", "a", "a", "c");
int count = Collections.frequency(list, "a");
// 3("a"出现3次)

indexOfSubList查找子列表

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

int index = Collections.indexOfSubList(list, sub);
// 1(第一次出现位置)

要点总结

  • Collections是java.util包的集合工具类
  • sort排序,reverse反转,shuffle打乱
  • binarySearch二分查找(需先排序)
  • max/min获取最大最小值
  • fill填充,replaceAll替换元素
  • unmodifiableList创建不可变集合
  • emptyList/emptySet返回空集合
  • synchronizedList创建线程安全集合
  • frequency统计元素出现次数

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

← 上一篇 Arrays
下一篇 → DateUtils
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

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

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