当前位置: 首页 > news >正文

JAVA基础:Collections 工具类实战指南-从排序到线程安全

在 Java 开发中,集合类几乎贯穿每一个项目,而Collections工具类提供了一系列强大的方法,用于操作和增强集合的功能。无论是排序、查找还是线程安全的封装,Collections工具类都是提升代码效率和质量的重要工具。

一、Collections 工具类概述

java.util.Collections是 Java 提供的一个工具类,主要用于操作集合类(如 List、Set 和 Map)。其核心方法包括:

1.排序操作:如sort()和reverseOrder()。

2.查找与填充:如binarySearch()和fill()。

3.其他操作:如shuffle()、swap()等。

4.线程安全包装:如synchronizedList()和synchronizedMap()。

5.不可变集合:如unmodifiableList()。

二、排序操作:让数据更有序

Collections.sort()是最常用的方法之一,可对 List 进行自然排序或自定义排序。

示例 1:自然排序

import java.util.*;public class CollectionsSortExample {public static void main(String[] args) {List<String> list = new ArrayList<>(Arrays.asList("banana", "apple", "cherry"));// 自然排序Collections.sort(list);System.out.println("自然排序后: " + list);  // 输出: [apple, banana, cherry]}
}

示例 2:自定义排序

Collections.sort(list, Comparator.reverseOrder());
System.out.println("降序排序后: " + list);  // 输出: [cherry, banana, apple]

实战场景:商品价格排序

假设有一个商品列表,需要按价格从低到高排序:

List<Product> products = Arrays.asList(new Product("Apple", 100),new Product("Banana", 60),new Product("Cherry", 120)
);Collections.sort(products, Comparator.comparingInt(Product::getPrice));
System.out.println("按价格排序后的商品: " + products);

三、查找与填充

1. Collections.binarySearch()

binarySearch()用于在排序后的列表中快速查找元素的位置。

import java.util.*;public class CollectionsBinarySearchExample {public static void main(String[] args) {List<Integer> list = Arrays.asList(1, 3, 5, 7, 9);// 必须先排序Collections.sort(list);int index = Collections.binarySearch(list, 5);System.out.println("元素 5 的索引位置: " + index);  // 输出: 2}
}

注意:

binarySearch()的前提是列表必须是有序的,否则结果不可预测。

2. Collections.fill()

fill()可将集合中的所有元素替换为指定值。

import java.util.*;
public class CollectionsFillExample {public static void main(String[] args) {List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C"));Collections.fill(list, "X");System.out.println("填充后的集合: " + list);  // 输出: [X, X, X]}
}

四、其他操作

1. Collections.shuffle()

shuffle()用于将集合中的元素随机排列,适合打乱顺序的场景,如洗牌程序或抽奖工具。

Collections.shuffle(list);
System.out.println("打乱顺序后的集合: " + list);

实战场景:洗牌功能实现

List<String> deck = new ArrayList<>(Arrays.asList("♥A", "♥2", "♥3", "♠A", "♠2", "♠3"
));
Collections.shuffle(deck);
System.out.println("洗牌后的结果: " + deck);

2. Collections.swap()

swap()方法可交换集合中两个元素的位置。

import java.util.*;public class SwapExample {public static void main(String[] args) {List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C"));Collections.swap(list, 0, 2);System.out.println("交换后的列表: " + list);  // 输出: [C, B, A]}
}

实战场景:数组中最小值和最大值交换

List<Integer> list = Arrays.asList(3, 5, 1, 9, 7);
int minIndex = list.indexOf(Collections.min(list));
int maxIndex = list.indexOf(Collections.max(list));
Collections.swap(list, minIndex, maxIndex);
System.out.println("交换后: " + list);  // 示例输出: [9, 5, 1, 3, 7]

五、线程安全包装

在多线程环境中,原生集合类如ArrayList并不是线程安全的,可以通过Collections.synchronizedXXX()方法生成线程安全的集合。

示例 1:线程安全的集合包装

import java.util.*;public class SynchronizedCollectionsExample {public static void main(String[] args) {List<String> list = new ArrayList<>();// 转换为线程安全的 ListList<String> synchronizedList = Collections.synchronizedList(list);synchronizedList.add("Thread-safe");System.out.println("线程安全的集合: " + synchronizedList);}
}

实战场景:多线程统计数据

List<Integer> scores = Collections.synchronizedList(new ArrayList<>());Runnable task = () -> {for (int i = 0; i < 1000; i++) {scores.add(i);}
};Thread t1 = new Thread(task);
Thread t2 = new Thread(task);t1.start();
t2.start();try {t1.join();t2.join();
} catch (InterruptedException e) {e.printStackTrace();
}System.out.println("线程安全的集合大小: " + scores.size());

六、不可变集合:安全共享数据

不可变集合是指在创建后无法修改的集合类型,适合在多线程环境下共享数据。

示例 1: 创建不可变集合

 unmodifiableList
List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C"));
List<String> unmodifiableList = Collections.unmodifiableList(list);// 尝试修改会抛出 UnsupportedOperationException
try {unmodifiableList.add("D");
} catch (UnsupportedOperationException e) {System.out.println("Cannot modify immutable list!");
}

实战场景:配置文件的共享

Map<String, String> config = new HashMap<>();
config.put("host", "localhost");
config.put("port", "8080");Map<String, String> unmodifiableConfig = Collections.unmodifiableMap(config);// 多个线程可以安全地读取配置
Runnable task = () -> {System.out.println("Host: " + unmodifiableConfig.get("host"));
};Thread t1 = new Thread(task);
Thread t2 = new Thread(task);t1.start();
t2.start();

七、性能优化建议

1.避免频繁排序

对于动态数据的排序,建议使用TreeSet或PriorityQueue。

2.选择合适的线程安全集合

在频繁读写场景中,优先考虑ConcurrentHash-Map或CopyOnWriteArrayList。

3.减少锁竞争

使用synchronizedList时,尽量避免在循环中频繁调用修改操作。

八、综合实战:线程安全排行榜

以下案例展示如何结合Collections工具类创建线程安全的排行榜:

import java.util.*;public class LeaderboardExample {public static void main(String[] args) {// 创建线程安全的排行榜(List)List<Integer> scores = Collections.synchronizedList(new ArrayList<>());// 添加分数scores.addAll(Arrays.asList(85, 90, 75, 95, 88));// 排序Collections.sort(scores, Collections.reverseOrder());System.out.println("排行榜: " + scores);  // 输出: [95, 90, 88, 85, 75]// 查找某个分数的排名int rank = Collections.binarySearch(scores, 90, Collections.reverseOrder());System.out.println("分数 90 的排名: 第 " + (rank + 1) + " 名");}
}

相关文章:

  • Vtable
  • 如何用postman进行批量操作
  • 根据用户出生日期计算年龄
  • 从暴力到优化:解决「分数严格小于k的子数组数目」问题
  • 代码随想录算法训练营第六十一天 | floyd算法
  • JAVA JVM面试题(二)
  • 【angular19】入门基础教程(二):组件的创建与使用
  • DeepSeek智能时空数据分析(六):大模型NL2SQL绘制城市之间连线
  • J-Link RTT打印输出调试信息
  • 普推知产:商标驳回复审下初步审定公告了!
  • 早投早发表!3本 Nature 新子刊已开放投稿系统!
  • 深度学习---框架流程
  • Redis的高性能奥秘:深入解析IO多路复用与单线程事件驱动模型
  • docker安装部署TDengine实现主从复制
  • 车载软件架构 --- AUTOSAR的方法论
  • 基于大模型的胆总管结石全流程预测与临床应用研究报告
  • kylin v10 + argo + ascend 310p多机多卡 pytorch distributed 训练
  • [Android] GKD v1.10.3
  • css中:is和:where 伪函数
  • 算力困局:AI 狂飙背后的能源枷锁与破局之道
  • 出行注意防晒补水,上海五一假期以多云天气为主最高33℃
  • 豆神教育:2024年净利润1.37亿元,同比增长334%
  • 中办、国办印发《安全生产考核巡查办法》
  • 直播电商行业代表呼吁:携手并肩伸出援手助力外贸企业攻坚克难
  • 发出“美利坚名存实亡”呼号的卡尼,将带领加拿大走向何方?
  • 北上广深还是小城之春?“五一”想好去哪玩了吗