SpringBoot 内置 CollectionUtil:实战用法速览,告别空指针!

📌 核心实用功能

CollectionUtil 是 SpringBoot 中超实用的集合工具类(spring-core 包自带),不用额外引入依赖,就能轻松搞定集合判空、转换、筛选等高频场景,今天就用🌰带你吃透这些实用功能!

判空:isEmpty/isNotEmpty(最常用)

转换:arrayToList(数组转集合)

合并:mergeArrayIntoCollection(集合 + 数组合并)

匹配:contains(元素存在判断)

兜底:emptyIfNull(避免 Null 返回)

📌 实战例子

1. 集合判空(最常用❗️)

痛点 :手动写 list == null || list.isEmpty() 又繁琐又容易漏解决方案CollectionUtil.isEmpty() / CollectionUtil.isNotEmpty()

java 复制代码
import org.springframework.util.CollectionUtils;
import java.util.List;

// 模拟业务场景:查询用户订单列表
List<String> orderIds = userService.getOrderIds(1001);

// 判空:空集合/Null都返回true
if (CollectionUtils.isEmpty(orderIds)) {
    System.out.println("❌ 暂无订单数据");
    return;
}

// 非空才处理
if (CollectionUtils.isNotEmpty(orderIds)) {
    System.out.println("✅ 订单数:" + orderIds.size());
}

2. 数组转集合(快速转换💨)

痛点 :数组转 List 还要手动 new ArrayList + 遍历,太麻烦解决方案CollectionUtils.arrayToList()

java 复制代码
import org.springframework.util.CollectionUtils;

// 模拟:接口返回数组格式的商品ID
String[] goodsIds = {"1001", "1002", "1003"};

// 一行转成List
List<String> goodsList = CollectionUtils.arrayToList(goodsIds);
System.out.println("📦 转换后的集合:" + goodsList); // [1001, 1002, 1003]

3. 集合合并(拼接两个集合🧩)

痛点 :合并两个 List 要循环 addAll,代码冗余解决方案CollectionUtils.mergeArrayIntoCollection()

java 复制代码
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;

// 模拟:购物车合并(本地购物车 + 线上购物车)
List<String> localCart = new ArrayList<>();
localCart.add("商品A");
localCart.add("商品B");

String[] onlineCart = {"商品C", "商品D"};

// 把数组合并到集合中
CollectionUtils.mergeArrayIntoCollection(onlineCart, localCart);
System.out.println("🛒 合并后购物车:" + localCart); // [商品A, 商品B, 商品C, 商品D]

4. 判读集合是否包含指定元素(精准匹配🎯)

痛点 :手动遍历判断元素是否存在,代码不优雅解决方案CollectionUtils.contains()

java 复制代码
import org.springframework.util.CollectionUtils;
import java.util.List;

// 模拟:判断用户是否在白名单内
List<String> whiteList = List.of("admin", "test", "user1");
String currentUser = "admin";

if (CollectionUtils.contains(whiteList.iterator(), currentUser)) {
    System.out.println("✅ 用户在白名单,允许操作");
} else {
    System.out.println("❌ 无权限,请联系管理员");
}

5. 空集合兜底(避免返回 Null🚫)

痛点 :方法返回 Null 集合,调用方容易空指针解决方案CollectionUtils.emptyIfNull()

java 复制代码
import org.springframework.util.CollectionUtils;
import java.util.List;

// 模拟:查询用户收藏列表(可能返回Null)
List<String> collectList = collectService.getCollects(1001);

// 兜底:如果是Null,返回空集合(而非Null)
List<String> safeList = CollectionUtils.emptyIfNull(collectList);
// 放心遍历,不会空指针
safeList.forEach(collect -> System.out.println("⭐ 收藏内容:" + collect));
相关推荐
dkbnull1 天前
深入理解Spring两大特性:IoC和AOP
spring boot
洋洋技术笔记1 天前
Spring Boot条件注解详解
java·spring boot
洋洋技术笔记2 天前
Spring Boot配置管理最佳实践
spring boot
用户8307196840823 天前
Spring Boot 项目中日期处理的最佳实践
java·spring boot
埃博拉酱3 天前
VS Code Remote SSH 连接 Windows 服务器卡在"下载 VS Code 服务器":prcdn DNS 解析失败的诊断与 BITS 断点续传
windows·ssh·visual studio code
大道至简Edward3 天前
Spring Boot 2.7 + JDK 8 升级到 Spring Boot 3.x + JDK 17 完整指南
spring boot·后端
洋洋技术笔记3 天前
Spring Boot启动流程解析
spring boot·后端
怒放吧德德4 天前
Spring Boot 实战:RSA+AES 接口全链路加解密(防篡改 / 防重放)
java·spring boot·后端
唐宋元明清21884 天前
.NET 本地Db数据库-技术方案选型
windows·c#
加号34 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql