一、前言
? ? ? ? 最近小永哥發現,在開發過程中,經常會遇到需要對list進行分組,就是假如有一個RecordTest對象集合,RecordTest對象都有一個type的屬性,需要將這個集合按type屬性進行分組,轉換為一個以type為key,RecordTest集合為value的Map對象,這個功能其實本身并不難,相信以老鐵們的實力那還不是輕輕松松嘛,那下面小永哥就先獻丑了。
二、代碼實現
2.1、常規實現
? ? ? ? 實現思路:通過創建一個Map,然后遍歷原RecordTest集合,在循環中先獲取到type屬性,然后判斷該type屬性在Map中是否存在,如果以存在,就從Map中通過key獲取到一個list,然后將當前本次循環所操作的RecordTest對象add到這個list中。如果該type在Map中不存在,那么就新建一個list,將本次循環的RecordTest對象add進去,然后再以type為key,list為value的方式put進Map中,最后我們就獲取到了一個以type分好組的Map對象了。
package com.relation;import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson2.JSON;
import org.junit.jupiter.api.Test;import java.util.List;
import java.util.Map;/*** @author huhy* @version 1.0* @Description:* @ClassName Date:2025/5/20 21:50*/
public class ListTest {@Testpublic void listGroupTest(){//模擬數據準備List<RecordTest> recordTestList = CollectionUtil.newArrayList();recordTestList.add(new RecordTest("型號A","item-code-A","item"));recordTestList.add(new RecordTest("型號B","item-code-B","item"));recordTestList.add(new RecordTest("文件A","file-code-B","file"));recordTestList.add(new RecordTest("文件B","file-code-B","file"));recordTestList.add(new RecordTest("文件C","file-code-C","file"));recordTestList.add(new RecordTest("圖片A","photo-code-A","photo"));recordTestList.add(new RecordTest("視頻A","video-code-C","video"));//按type屬性進行分組Map<String,List<RecordTest>> recordTestGroupMap = MapUtil.newHashMap();for (RecordTest recordTest : recordTestList) {String type = recordTest.getType();List<RecordTest> recordTests;if(recordTestGroupMap.containsKey(type)){recordTests = recordTestGroupMap.get(type);}else {recordTests = CollectionUtil.newArrayList();recordTestGroupMap.put(type,recordTests);}recordTests.add(recordTest);}//打印System.out.println(JSON.toJSONString(recordTestGroupMap));}class RecordTest{private String name;private String code;private String type;public RecordTest(String name, String code, String type) {this.name = name;this.code = code;this.type = type;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getCode() {return code;}public void setCode(String code) {this.code = code;}public String getType() {return type;}public void setType(String type) {this.type = type;}}}
????????
????????打印出的信息不太方便觀看,我們找個工具將JSON格式化一下,順便提一句,有時候我們需要將JSON文本格式化的時候,是不是冷不丁還想不到什么好的方式,特別是開發環境還是內網的時候,沒辦法上網在線轉?其實不管是內網還是外網開發環境,我們作為一個后端開發人員,postman可以說是標配,畢竟還得自測接口嘛,這時候postman就能發揮作用了,postman的post請求在輸入參數以后,有一個Beautify的按鈕,輕輕一點,JSON文本就格式化好了,非常方便。
? ? ? ? 這點小玩意兒雖然順利實現了,而且實現代碼也不孬,但是距離優雅還是差不少,下面我們換種嗨皮的實現方式。
2.2、優雅實現
? ? ? ??
@Testpublic void listGroupTest(){//模擬數據準備List<RecordTest> recordTestList = CollectionUtil.newArrayList();recordTestList.add(new RecordTest("型號A","item-code-A","item"));recordTestList.add(new RecordTest("型號B","item-code-B","item"));recordTestList.add(new RecordTest("文件A","file-code-B","file"));recordTestList.add(new RecordTest("文件B","file-code-B","file"));recordTestList.add(new RecordTest("文件C","file-code-C","file"));recordTestList.add(new RecordTest("圖片A","photo-code-A","photo"));recordTestList.add(new RecordTest("視頻A","video-code-C","video"));//按type屬性進行分組Map<String,List<RecordTest>> recordTestGroupMap = recordTestList.stream().collect(Collectors.groupingBy(RecordTest::getType));//打印System.out.println(JSON.toJSONString(recordTestGroupMap));}
三、結語
? ? ? ? 通過jdk1.8特性一行實現分組功能,代碼量瞬間減少了10行,雖然現在節省的10行不算什么,但是積少成多,等真正開發的時候,我們可以把學到的東西都盡可能的付諸于實踐,這樣節省的就不僅僅是10行,可能就是成千上萬行,我們的代碼也會越來越精煉。