当前位置: 首页 > wzjs >正文

做最好的美食分享网站互联网产品推广

做最好的美食分享网站,互联网产品推广,网站为什么具有网络营销价值,国内外贸平台一、前言 最近小永哥发现,在开发过程中,经常会遇到需要对list进行分组,就是假如有一个RecordTest对象集合,RecordTest对象都有一个type的属性,需要将这个集合按type属性进行分组,转换为一个以type为key&…

一、前言

        最近小永哥发现,在开发过程中,经常会遇到需要对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行,可能就是成千上万行,我们的代码也会越来越精炼。

http://www.dtcms.com/wzjs/196128.html

相关文章:

  • 网站开发的中期工作seo优化网站网页教学
  • wordpress订阅插件上海seo有哪些公司
  • 中兴建设 基金管理有限公司网站线上运营推广
  • dz论坛网站建设百度小说搜索风云榜排行榜
  • 好的高端企业网站建设公司怎样进行seo
  • 南昌网站备案网站推广引流最快方法
  • 凡科做的网站怎么改壁纸推广和竞价代运营
  • 换网站了吗品牌推广公司
  • 上海php网站开发公司域名解析
  • 关于旅行的网站怎样做怎么让网站排名上去
  • 企业网站seo服务靠谱seo整站优化外包
  • 自己创办网站央视新闻的新闻
  • 能够做代理的网站有哪些无锡seo培训
  • 青州做网站的网络公司网站搭建平台都有哪些
  • 做搬家网站推广在那好深圳网站seo推广
  • asp.net获取网站地址seo系统是什么
  • 网站运营推广难做吗seo关键词排名报价
  • 无为县建设局网站网络营销策划怎么写
  • 裕顺网站建设今日热点新闻事件
  • ps 矢量素材网站站长工具网址查询
  • 营销型网站建设网站手机免费推广途径与原因
  • 难道做网站的工资都不高吗中国网民博客 seo
  • 网站建设需求模版网站制作郑州
  • 手机自适应网站建设维护网络运营怎么学
  • 深圳宝安区做网站企业网站seo诊断工具
  • 湛江网站建设技术托管如何去除痘痘效果好
  • 企业网站建设规划应用关键词优化
  • b2c网站html营销软文300字
  • 自己有网站 做app吗哪些平台可以发布软文
  • 不备案如何架设网站google搜索中文入口