我不是Java Lambda的专家,所以我需要你的帮助。
如何提取以下列表:
[
{
"code": 1,
"nestedList": [{
"value": "A",
"count": 1
}]
},
{
"code": 2,
"nestedList": [{
"value": "A",
"count": 1
}]
},
{
"code": 3,
"nestedList": [{
"value": "A",
"count": 1
}]
},
{
"code": 4,
"nestedList": [{
"value": "B",
"count": 1
}]
},
{
"code": 5,
"nestedList": [{
"value": "B",
"count": 1
}]
]
以下一个:
[
{
"value": "A",
"count": 3
},
{
"value": "B",
"count": 2
}
]
我尝试了分组,过滤和其他操作但没有任何效果.....
因为我使用以下代码:
public class MainObject {
List<NestedObject> nestedList;
//getters and setters
}
public class NestedObject {
private String value;
private Integer count;
//getters and setters
}
List<MainObject> mainObjectList = new ArrayList<>();
List<NestedObject> nestedObjects = new ArrayList<>();
NestedObject nestedObject = new NestedObject();
nestedObject.setValue("A");
nestedObject.setCount(3);
nestedObjects.add(nestedObject);
MainObject mainObject = new MainObject();
mainObject.setNestedList(nestedObjects);
mainObjectList.add(mainObject);
List<NestedObject> nestedList2 = new ArrayList<>();
NestedObject nestedObject2 = new NestedObject();
nestedObject2.setValue("A");
nestedObject2.setCount(2);
nestedList2.add(nestedObject2);
MainObject mainObject2 = new MainObject();
mainObject2.setNestedList(nestedList2);
mainObjectList.add(mainObject2);
List<NestedObject> nestedList3 = new ArrayList<>();
NestedObject nestedObject3 = new NestedObject();
nestedObject3.setValue("B");
nestedObject3.setCount(8);
nestedList3.add(nestedObject3);
MainObject mainObject3 = new MainObject();
mainObject3.setNestedList(nestedList3);
mainObjectList.add(mainObject3);
List<NestedObject> nestedList4 = new ArrayList<>();
NestedObject nestedObject4 = new NestedObject();
nestedObject4.setValue("B");
nestedObject4.setCount(2);
nestedList4.add(nestedObject4);
MainObject mainObject4 = new MainObject();
mainObject4.setNestedList(nestedList4);
mainObjectList.add(mainObject4);
System.out.println(mainObjectList.size());
List<String> listInteger =
mainObjectList.stream()
.map(mainObject -> mainObject.getNestedList())
.flatMap(List::stream)
.map(NestedObject::getValue)
.collect(NestedObject::getValue);
我试图提取values
,然后将count
与另一个foreach
联系起来。
我搜索了像underscore.js这样的东西。但我真的不知道......
答案 0 :(得分:2)
查看预期结果,不清楚是否要在分组后添加同一存储桶中所有对象的count
属性,或者仅在分组后添加每个存储桶中元素的计数。
因此,我将提供几种解决方案,然后您可以根据需要决定。
如果要按value
进行分组,请将同一存储桶中所有对象的count
属性相加:
Map<String, Integer> resultSet = mainObjectList.stream()
.map(MainObject::getNestedList)
.flatMap(List::stream)
.collect(Collectors.groupingBy(NestedObject::getValue,
Collectors.summingInt(NestedObject::getCount)));
或者如果您想将结果收回到列表中累积的NestedObject
类型中:
List<NestedObject> resulSet = mainObjectList.stream()
.map(MainObject::getNestedList)
.flatMap(List::stream)
.collect(Collectors.groupingBy(NestedObject::getValue,
Collectors.summingInt(NestedObject::getCount)))
.entrySet()
.stream()
.map(e -> {
NestedObject obj = new NestedObject();
obj.setCount(e.getValue());
obj.setValue(e.getKey());
return obj;
}).collect(Collectors.toList());
如果要按value
进行分组,则计算同一存储桶中的数字对象:
Map<String, Long> resultSet = mainObjectList.stream()
.map(MainObject::getNestedList)
.flatMap(List::stream)
.collect(Collectors.groupingBy(NestedObject::getValue,
Collectors.counting()));
或者如果您想将结果收回到列表中累积的NestedObject
类型中:
List<NestedObject> resultSet = mainObjectList.stream()
.map(MainObject::getNestedList)
.flatMap(List::stream)
.collect(Collectors.groupingBy(NestedObject::getValue,
Collectors.summingInt(e -> 1)))
.entrySet()
.stream()
.map(e -> {
NestedObject obj = new NestedObject();
obj.setCount(e.getValue());
obj.setValue(e.getKey());
return obj;
}).collect(Collectors.toList());