最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot 內(nèi)置 CollectionUtil用法實戰(zhàn)指南

 更新時間:2025年12月24日 09:31:48   作者:Moshow鄭鍇  
文章介紹了SpringBoot中自帶的CollectionUtil類,包括判空、轉(zhuǎn)換、合并、匹配和兜底功能,并通過實戰(zhàn)例子詳細展示了這些功能的應(yīng)用場景和優(yōu)勢,感興趣的朋友跟隨小編一起看看吧

?? 核心實用功能

CollectionUtil 是 SpringBoot 中超實用的集合工具類(spring-core 包自帶),不用額外引入依賴,就能輕松搞定集合判空、轉(zhuǎn)換、篩選等高頻場景,今天就用??帶你吃透這些實用功能!

? 判空:isEmpty/isNotEmpty(最常用)

? 轉(zhuǎn)換:arrayToList(數(shù)組轉(zhuǎn)集合)

? 合并:mergeArrayIntoCollection(集合 + 數(shù)組合并)

? 匹配:contains(元素存在判斷)

? 兜底:emptyIfNull(避免 Null 返回)

?? 實戰(zhàn)例子

1. 集合判空(最常用??)

痛點:手動寫 list == null || list.isEmpty() 又繁瑣又容易漏解決方案CollectionUtil.isEmpty() / CollectionUtil.isNotEmpty()

import org.springframework.util.CollectionUtils;
import java.util.List;
// 模擬業(yè)務(wù)場景:查詢用戶訂單列表
List<String> orderIds = userService.getOrderIds(1001);
// 判空:空集合/Null都返回true
if (CollectionUtils.isEmpty(orderIds)) {
    System.out.println("? 暫無訂單數(shù)據(jù)");
    return;
}
// 非空才處理
if (CollectionUtils.isNotEmpty(orderIds)) {
    System.out.println("? 訂單數(shù):" + orderIds.size());
}

2. 數(shù)組轉(zhuǎn)集合(快速轉(zhuǎn)換??)

痛點:數(shù)組轉(zhuǎn) List 還要手動 new ArrayList + 遍歷,太麻煩解決方案CollectionUtils.arrayToList()

import org.springframework.util.CollectionUtils;
// 模擬:接口返回數(shù)組格式的商品ID
String[] goodsIds = {"1001", "1002", "1003"};
// 一行轉(zhuǎn)成List
List<String> goodsList = CollectionUtils.arrayToList(goodsIds);
System.out.println("?? 轉(zhuǎn)換后的集合:" + goodsList); // [1001, 1002, 1003]

3. 集合合并(拼接兩個集合??)

痛點:合并兩個 List 要循環(huán) addAll,代碼冗余解決方案CollectionUtils.mergeArrayIntoCollection()

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"};
// 把數(shù)組合并到集合中
CollectionUtils.mergeArrayIntoCollection(onlineCart, localCart);
System.out.println("?? 合并后購物車:" + localCart); // [商品A, 商品B, 商品C, 商品D]

4. 判讀集合是否包含指定元素(精準匹配??)

痛點:手動遍歷判斷元素是否存在,代碼不優(yōu)雅解決方案CollectionUtils.contains()

import org.springframework.util.CollectionUtils;
import java.util.List;
// 模擬:判斷用戶是否在白名單內(nèi)
List<String> whiteList = List.of("admin", "test", "user1");
String currentUser = "admin";
if (CollectionUtils.contains(whiteList.iterator(), currentUser)) {
    System.out.println("? 用戶在白名單,允許操作");
} else {
    System.out.println("? 無權(quán)限,請聯(lián)系管理員");
}

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

痛點:方法返回 Null 集合,調(diào)用方容易空指針解決方案CollectionUtils.emptyIfNull()

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("? 收藏內(nèi)容:" + collect));

到此這篇關(guān)于SpringBoot 內(nèi)置 CollectionUtil用法實戰(zhàn)指南的文章就介紹到這了,更多相關(guān)SpringBoot 內(nèi)置 CollectionUtil用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

大丰市| 嘉定区| 都兰县| 沈丘县| 长沙市| 三都| 尤溪县| 敦化市| 江油市| 定西市| 桃园县| 尚义县| 长宁县| 陆河县| 南召县| 天等县| 南昌县| 河曲县| 墨玉县| 资溪县| 射阳县| 莱西市| 吴忠市| 葵青区| 樟树市| 西峡县| 济阳县| 商水县| 无锡市| 安徽省| 灌阳县| 读书| 江达县| 嘉义市| 筠连县| 福州市| 奉新县| 图木舒克市| 文成县| 肥城市| 张掖市|