日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java8中的Stream流式操作总结,List转Map或List转LinkedHashMap使用Collectors.groupingBy用法

發(fā)布時(shí)間:2024/1/8 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java8中的Stream流式操作总结,List转Map或List转LinkedHashMap使用Collectors.groupingBy用法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言背景描述:


開(kāi)發(fā)使用本來(lái)是直接使用數(shù)據(jù)庫(kù)的依據(jù)SQL進(jìn)行g(shù)roup By獲取到數(shù)據(jù)表的分組的一個(gè)字段的字符串,可是后來(lái)字符串越來(lái)越多越長(zhǎng),導(dǎo)致的最后的后面長(zhǎng)度超多1024個(gè)漢字就會(huì)被截取,所以需要重新的重構(gòu)方案進(jìn)行字符串處理。

原始方案:
SELECT ?GROUP_CONCAT(t.factor_contant) ?as 'factor_contants' ?, ?t.factor_value ? as ?'factor_value' ?from ? tbm_factor_config ?t ? ?where ?t.factor_type=5 ?GROUP BY ?t.factor_value;

得到的結(jié)果:

最后的factor_contants 字段超長(zhǎng)了,導(dǎo)致后續(xù)的字符創(chuàng)獲取不到,會(huì)導(dǎo)致配置失效!

修改方案為L(zhǎng)ist獲取數(shù)據(jù)表數(shù)據(jù),order? by 之后進(jìn)行List使用流式Stream轉(zhuǎn)成LinkedHashMap,然后返回配置就可以的。

JDK8使用Stream的把List使用流式Stream轉(zhuǎn)成LinkedHashMap


Map<Integer, List<TbmFactorConfig>> tbmFactorConfigMap = tbmFactorConfigList.stream().collect(Collectors.groupingBy(TbmFactorConfig::getFactorValue, LinkedHashMap::new, Collectors.toList()));
?

代碼效果:

@Overridepublic List<TbmFactorConfig> getFactorConfigByGourp(Integer fileType) {List<TbmFactorConfig> tbmFactorConfigList = tbmFactorConfigMapper.getFactorListByGroup(fileType);if (CollectionUtils.isEmpty(tbmFactorConfigList)) {return null;}Map<Integer, List<TbmFactorConfig>> tbmFactorConfigMap = tbmFactorConfigList.stream().collect(Collectors.groupingBy(TbmFactorConfig::getFactorValue, LinkedHashMap::new, Collectors.toList()));List<TbmFactorConfig> resultTbmFactorConfig = new ArrayList<TbmFactorConfig>();tbmFactorConfigMap.keySet().forEach(item -> {TbmFactorConfig tbmFactorConfig = new TbmFactorConfig();tbmFactorConfig.setFactorValue(item);String factorValues = tbmFactorConfigMap.get(item).stream().map(factorConfig -> factorConfig.getFactorContant()).collect(Collectors.joining(","));tbmFactorConfig.setFactorContant(factorValues);tbmFactorConfig.setFactorWeight(tbmFactorConfigList.get(0).getFactorWeight());resultTbmFactorConfig.add(tbmFactorConfig);log.error("目前獲取到的map的key: {},獲取到的value是:{} ", item, factorValues);});return resultTbmFactorConfig;}

打印出來(lái)的日志:

FactorConfigDaoImpl :目前獲取到的map的key: ?80,獲取到的value是:FX168北美,TechWeb,北京日?qǐng)?bào),花網(wǎng)專稿,中國(guó)新聞網(wǎng),中華工商時(shí)報(bào) ?
FactorConfigDaoImpl :目前獲取到的map的key: ?85,獲取到的value是:中國(guó)基金報(bào) ?
FactorConfigDaoImpl :目前獲取到的map的key: ?87,獲取到的value是:蓋世汽車,蓋世汽車快訊,中國(guó)汽車工業(yè)信息網(wǎng) ?
FactorConfigDaoImpl :目前獲取到的map的key: ?88,獲取到的value是:機(jī)床商務(wù)網(wǎng),中鋼網(wǎng),中國(guó)化工報(bào),中國(guó)數(shù)字視聽(tīng)網(wǎng),中國(guó)水網(wǎng) ?
FactorConfigDaoImpl :目前獲取到的map的key: ?90,獲取到的value是:環(huán)球網(wǎng),中國(guó)網(wǎng)科技,北京商報(bào),中財(cái)網(wǎng),中國(guó)機(jī)床工具工業(yè)協(xié)會(huì) ?
FactorConfigDaoImpl :目前獲取到的map的key: ?91,獲取到的value是:中關(guān)村在線 ?
FactorConfigDaoImpl :目前獲取到的map的key: ?92,獲取到的value是:全國(guó)企業(yè)破產(chǎn)重整案件信息網(wǎng) ?
FactorConfigDaoImpl :目前獲取到的map的key: ?94,獲取到的value是:格隆匯,深圳商報(bào) ?

完美的實(shí)現(xiàn)SQL的方案替換
?

總結(jié)

以上是生活随笔為你收集整理的java8中的Stream流式操作总结,List转Map或List转LinkedHashMap使用Collectors.groupingBy用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。