-
小弟尝试将一段如下json读到一个Map<String, List<String>>中,map
的key为condName,map的value为condValue
现在用stream结合Collectors的groupingBy和toList方法,将List<Condition>转成了Map<String, List<Condition>>
然后尝试通过mapping方法将List<Condition>映射为List<String>时发现无从下手。。。几次尝试后得到了一个Map<String, List<List<String>>>,多了一层List
想去掉这一层List,如果再遍历一遍又感觉不太优雅
求助各位有什么思路吗,最好是通过mapping一次得到想要的结果
不胜感激!
问题出现的环境背景及自己尝试过哪些方法
"condition": [
"condName": "name1",
"condValue": [
"val11",
"val12"
"condName": "name2",
"condValue": [
"val21"
"condName": "name3",
"condValue": [
"val31",
"val32",
"val33"
"total": 3
@Data
public class Request {
private List<Condition> condition;
private Long total;
@Data
public class Condition {
private String condName;
private List<String> condValue;
转换方法(读取json和转成Request对象的代码省略。。。):
String json = getContent();
try {
List<Condition> conditions = deseriliaze(json).getCondition();
Map<String, List<Condition>> map1 = conditions.stream().collect(Collectors.groupingBy(Condition::getCondName));
Map<String, List<List<String>>> map2 = conditions.stream().collect(Collectors.groupingBy(Condition::getCondName, Collectors.mapping(Condition::getCondValue, Collectors.toList())));
} catch (Exception e) {
e.printStackTrace();
你期待的结果是什么?实际看到的错误信息又是什么?
{name3=[Condition(condName=name3, condValue=[val31, val32, val33])], name2=[Condition(condName=name2, condValue=[val21])], name1=[Condition(condName=name1, condValue=[val11, val12])]}
{name3=[[val31, val32, val33]], name2=[[val21]], name1=[[val11, val12]]}