php 读取网站文件,平东网站建设,潍坊百度搜索优化,微信开店小程序怎么弄一、前言 最近小永哥发现#xff0c;在开发过程中#xff0c;经常会遇到需要对list进行分组#xff0c;就是假如有一个RecordTest对象集合#xff0c;RecordTest对象都有一个type的属性#xff0c;需要将这个集合按type属性进行分组#xff0c;转换为一个以type为key在开发过程中经常会遇到需要对list进行分组就是假如有一个RecordTest对象集合RecordTest对象都有一个type的属性需要将这个集合按type属性进行分组转换为一个以type为keyRecordTest集合为value的Map对象这个功能其实本身并不难相信以老铁们的实力那还不是轻轻松松嘛那下面小永哥就先献丑了。
二、代码实现
2.1、常规实现 实现思路通过创建一个Map然后遍历原RecordTest集合在循环中先获取到type属性然后判断该type属性在Map中是否存在如果以存在就从Map中通过key获取到一个list然后将当前本次循环所操作的RecordTest对象add到这个list中。如果该type在Map中不存在那么就新建一个list将本次循环的RecordTest对象add进去然后再以type为keylist为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(){//模拟数据准备ListRecordTest 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属性进行分组MapString,ListRecordTest recordTestGroupMap MapUtil.newHashMap();for (RecordTest recordTest : recordTestList) {String type recordTest.getType();ListRecordTest 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(){//模拟数据准备ListRecordTest 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属性进行分组MapString,ListRecordTest recordTestGroupMap recordTestList.stream().collect(Collectors.groupingBy(RecordTest::getType));//打印System.out.println(JSON.toJSONString(recordTestGroupMap));} 三、结语 通过jdk1.8特性一行实现分组功能代码量瞬间减少了10行虽然现在节省的10行不算什么但是积少成多等真正开发的时候我们可以把学到的东西都尽可能的付诸于实践这样节省的就不仅仅是10行可能就是成千上万行我们的代码也会越来越精炼。