List -> Map> demo
java 8StreamListMap
程序员文章站
2022-07-12 18:33:00
...
package com.ctrip.framework.apollo.demo; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * @author maxiaolong * @create 2018-07-17 10:12 **/ public class StreamDemo { public static void main (String[] args) { convert(); } private static void convert () { List<Bean> beanList = Lists.newArrayList(new Bean("k1", "f1", "v1"), new Bean("k1", "f2", "v2"), new Bean("k2", "f2", "v3")); Map<String, Map<String, String>> resultMap = beanList.stream().collect(Collectors.toMap(bean -> bean.getKey(), bean -> { Map map = Maps.newHashMap(); map.put(bean.getField(), bean.getValue()); return map; }, (oldValue, newValue) -> { oldValue.putAll(newValue); return oldValue; })); resultMap.forEach((key, value) -> System.out.println("key: " + key + "; value: " + value)); } static class Bean { private String key; private String field; private String value; public Bean (String key, String field, String value) { this.key = key; this.value = value; this.field = field; } public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getField() { return field; } public void setField(String field) { this.field = field; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } } }
推荐阅读
-
List -> Map
> demo java 8StreamListMap -
jdbc查询返回来的List
-
mybatis循环遍历map中list、string、实体对象
-
Hibernate 返回List<Map<String,Object>>
-
Mybatis传入 List<Map<String,Object>>的入参
-
mybatis 返回Map<String,List<Entity>> 格式数据
-
mybatis 插入List<Map<String, Object>> 出现__frch_list_0异常
-
mybatis 批量插入list<map<String,String>>
-
mybatis_批量插入参数List_Map_String,Object
-
java Map<String,?> 和Map<?,?> 的使用/什么意思,以及用法,实战。