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));
相关推荐
Flittly5 小时前
【AgentScope Java新手村系列】(16)从RAG到多路检索
java·spring boot·spring
人活一口气10 小时前
从JVM调优到MCP协议:Java全栈技术体系深度总结与企业级架构实践
java·spring boot
Java陈序员1 天前
企业级!一个基于 Java 开发的开源 AI 应用开发平台!
spring boot·agent·mcp
杨运交1 天前
[041][公共模块]分布式唯一ID生成器设计与实现:一款灵活可扩展的雪花算法框架
spring boot
Flittly2 天前
【AgentScope Java新手村系列】(14)人机交互
java·spring boot·spring
Flynt3 天前
从Spring Boot 4.0升到4.1,我在Maven和gRPC上栽了跟头
java·spring boot·后端
掉鱼的猫4 天前
Spring Boot → Solon 注解迁移实战指南:一张对照表说清楚
java·spring boot
人活一口气5 天前
Spring Boot与AIGC的完美结合:从零搭建智能内容生成平台
java·spring boot·aigc
java小白小8 天前
SpringBoot(01): 初识SpringBoot,从Spring的痛点说起
spring boot
用户3169353811838 天前
如何从零编写一个 Spring Boot Starter
spring boot