{
"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]]}
1
Raymon111111 2018-10-17 02:00:09 +08:00
感觉你需要的是 Collectors.toCollection 这个
|
2
SoloCompany 2018-10-17 03:24:26 +08:00
groupingBy 需要指定 downstream Collector 而不是默认的 toList
downstream Collector 需要使用 reducer (reducing) reducer 把 Condition 先 map 成 List<String> 然后再通过 addAll 拼接(reduce)成新的 List<String> |
3
nl101531 2018-10-17 07:55:16 +08:00 1
直接 toMap 就可以变成 String, List<String>了。
```java Map<String, List<String>> listMap = conditions.stream() .collect(Collectors.toMap( Condition::getCondName, Condition::getCondValue, (v1, v2) -> {v1.addAll(v2);return v1;})); ``` |
5
519718366 2018-10-17 17:49:56 +08:00
nl101531 这种写法我个人觉得不太灵活,因为 toMap 的第三个参数 mergeFunction 只能返回 List 类型,如果楼主有对最后的这个集合采用 set 进行去重,那就不太方便了,我个人推荐下面这种写法 https://stackoverflow.com/questions/32252628/how-to-use-java-8-stream-and-lambda-to-flatmap-a-groupingby-result/32253225
核心就是进行 groupingby 操作时,downloadstream 进行转换操作 conditions.stream().collect(Collectors.groupingBy(Condition::getCondName, Collectors.mapping(Condition::getCondValue, Collector.of(ArrayList::new, List::addAll, (x, y) -> {x.addAll(y);return x;}))); 核心就是 Collector.of(...)。如果你提供 ArrayList,那最后就是 List<String>,如果你提供 HashSet 那最后就是 Set<String> |