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

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) + " 名");}
}
http://www.xdnf.cn/news/2657.html

相关文章:

  • ViTa-Zero:零样本视觉触觉目标 6D 姿态估计
  • Ubuntu深度学习革命:NVIDIA-Docker终极指南与创新实践
  • LLVIP、KAIST、M3FD数据集
  • GD32F407单片机开发入门(十六)单片机IAP(在应用编程)详解及实战源码
  • 消息队列优化指南:处理堆积与保障消息可靠性
  • 喜马拉雅卖身腾讯音乐:在线音频独立时代的终结
  • Molex莫仕连接器:增强高级驾驶辅助系统,打造更安全的汽车
  • codeforces C. The Trail
  • 【Nginx】 使用least_conn负载均衡算法是否能将客户端的长连接分散到不同的服务器上demo
  • 【AI生产力工具】Windsurf,一款AI编程工具
  • 华纳云:centos如何实现JSP页面的动态加载
  • 模板方法模式(Template Method Pattern)
  • 数据库对象概述
  • Java项目与技术栈场景题深度解析
  • C语言(5)—操作符详解
  • leetcode 143. 重排链表
  • js day8
  • Java学习手册: IoC 容器与依赖注入
  • leetcode刷题日记——两数相加
  • 【Redis】基础4:作为分布式锁
  • 搭建speak yarn集群:从零开始的详细指南
  • 关于健身房管理系统前后端软件开发主要功能需求分析
  • 深入理解网络原理:TCP协议详解
  • MCP Servers玩玩WebUI自动化
  • 如何在idea 中写spark程序
  • UARA串口开发基础
  • Dify+DeepSeek实战教程!企业级 AI 文档库本地化部署,数据安全与智能检索我都要
  • OpenResty技术深度解析:原理、应用与生态对比-优雅草卓伊凡
  • 基于 BERT 微调一个意图识别(Intent Classification)模型
  • LinuxAgent开源程序是一款智能运维助手,通过接入 DeepSeek API 实现对 Linux 终端的自然语言控制,帮助用户更高效地进行系统运维工作