Collections 工具类
基本介绍
(1)Collections 中提供了一系列静态方法对集合元素进行排序,查询和修改等操作
(2)操作对象:集合
常用方法
| 方法 | 描述 |
|---|---|
| sort() | 对 List 中的元素按自然顺序进行排序,也可以传入 Comparator 对 List 进行定制排序 |
| reverse() | 反转 List 中元素的顺序 |
| max() | 返回集合中的最大元素,可以传入比较器指定最大的含义 |
| min() | 返回集合中的最小元素,可以传入比较器指定最小的含义 |
| fill(list,填充值) | 将 List 中的所有元素设置为指定的值 |
| replaceAll(list,被替换元素,替换值) | 用指定的操作替换 List 中的每个元素 |
| copy(目标集合,源集合) | 把源集合元素复制到目标集合中,目标集合大小需要和源集合大小一致 |
| swap(list,元素 1 索引,元素 2 索引) | 交换 List 中指定位置的元素 |
| shuffle() | 随机打乱 List 中元素的顺序(即随机排序) |
| frequency(list,元素) | 返回指定元素在集合中出现的次数 |
代码示例
java
import java.util.*;
public class ListExample {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>(Arrays.asList(5, 2, 8, 1, 4));
// sort() - 排序
Collections.sort(list);
System.out.println("Sorted List: " + list);
// reverse() - 反转顺序
Collections.reverse(list);
System.out.println("Reversed List: " + list);
// max() - 获取最大元素
Integer max = Collections.max(list);
System.out.println("Max element: " + max);
// min() - 获取最小元素
Integer min = Collections.min(list);
System.out.println("Min element: " + min);
// fill() - 用指定值填充 List
Collections.fill(list, 10);
System.out.println("List after fill: " + list);
// replaceAll() - 替换所有元素
Collections.replaceAll(list, 10, 5);
System.out.println("List after replaceAll: " + list);
// copy() - 复制集合
List<Integer> copiedList = new ArrayList<>(list.size());
Collections.copy(copiedList, list);
System.out.println("Copied List: " + copiedList);
// swap() - 交换位置
Collections.swap(list, 0, 2);
System.out.println("List after swap: " + list);
// shuffle() - 随机打乱顺序
Collections.shuffle(list);
System.out.println("Shuffled List: " + list);
// frequency() - 计算元素频率
int frequency = Collections.frequency(list, 5);
System.out.println("Frequency of 5: " + frequency);
}
}
// 输出结果
Sorted List: [1, 2, 4, 5, 8]
Reversed List: [8, 5, 4, 2, 1]
Max element: 8
Min element: 1
List after fill: [10, 10, 10, 10, 10]
List after replaceAll: [5, 5, 5, 5, 5]
Copied List: [5, 5, 5, 5, 5]
List after swap: [5, 5, 5, 5, 5]
Shuffled List: [5, 5, 5, 5, 5]
Frequency of 5: 5copy()
底层源码
java
public static <T> void copy(List<? super T> dest, List<? extends T> src) {
int srcSize = src.size();
if (srcSize > dest.size())
throw new IndexOutOfBoundsException("Source does not fit in dest");
if (srcSize < COPY_THRESHOLD ||
(src instanceof RandomAccess && dest instanceof RandomAccess)) {
for (int i=0; i<srcSize; i++)
dest.set(i, src.get(i));
} else {
ListIterator<? super T> di=dest.listIterator();
ListIterator<? extends T> si=src.listIterator();
for (int i=0; i<srcSize; i++) {
di.next();
di.set(si.next());
}
}
}注意事项
(1)首先计算源结合的大小,如果源集合的大小大于了目标集合的大小就会抛出异常 IndexOutOfBoundsException
(2)复制之前需要对目标集合扩容,大小和源集合大小一致
代码示例
java
public class pra {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
ArrayList list = new ArrayList();
for (int i = 0; i < 10; i++) {
arrayList.add(i);
}
for (int i = 0; i < 10; i++) {
list.add("");
}
Collections.copy(list,arrayList);
System.out.println("arrayList:" + arrayList);
System.out.println("list:" + list);
}
}
// 输出结果
arrayList:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]