提取属性模式,合并并使用JavaLambda构建列表
我不是Java Lambda方面的专家,所以我需要你的帮助 如何提取以下列表:提取属性模式,合并并使用JavaLambda构建列表,lambda,java-8,Lambda,Java 8,我不是Java Lambda方面的专家,所以我需要你的帮助 如何提取以下列表: [ { "code": 1, "nestedList": [{ "value": "A", "count": 1 }] }, { "code": 2, "nestedList": [{ "value": "A", "count": 1 }] },
[
{
"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);
public类main对象{
列表嵌套列表;
//接球手和接球手
}
公共类嵌套对象{
私有字符串值;
私有整数计数;
//接球手和接球手
}
List mainObjectList=新建ArrayList();
List nestedObjects=new ArrayList();
NestedObject NestedObject=新建NestedObject();
nestedObject.setValue(“A”);
nestedObject.setCount(3);
添加(nestedObject);
MainObject MainObject=新的MainObject();
main object.setNestedList(嵌套对象);
mainObjectList.add(mainObject);
List nestedList2=新的ArrayList();
NestedObject NestedObject 2=新的NestedObject();
嵌套对象2.设置值(“A”);
nestedObject2.setCount(2);
添加(nestedObject2);
MainObject mainObject2=新的MainObject();
mainObject2.setNestedList(nestedList2);
mainObjectList.add(mainObject2);
List nestedList3=新的ArrayList();
NestedObject NestedObject 3=新的NestedObject();
嵌套对象3.设置值(“B”);
nestedObject3.setCount(8);
添加(nestedObject3);
MainObject mainObject3=新的MainObject();
mainObject3.setNestedList(nestedList3);
mainObjectList.add(mainObject3);
List nestedList4=新的ArrayList();
NestedObject NestedObject 4=新的NestedObject();
嵌套对象4.设置值(“B”);
nestedObject4.setCount(2);
添加(nestedObject4);
MainObject mainObject4=新的MainObject();
mainObject4.setNestedList(nestedList4);
mainObjectList.add(mainObject4);
System.out.println(mainObjectList.size());
列表整数=
mainObjectList.stream()
.map(mainObject->mainObject.getNestedList())
.flatMap(列表::流)
.map(NestedObject::getValue)
.collect(NestedObject::getValue);
我试图提取值
,然后用另一个foreach
获取计数
我搜索了类似下划线.js的内容。但我真的不知道
查看预期结果,不清楚是要在分组后添加同一个bucket中所有对象的
count
属性,还是只添加分组后每个bucket中元素的计数
因此,我将提供几个解决方案,然后您可以决定一个您想要的
如果要按值进行分组
,则对同一存储桶中所有对象的计数
属性求和:
Map<String, Integer> resultSet = mainObjectList.stream()
.map(MainObject::getNestedList)
.flatMap(List::stream)
.collect(Collectors.groupingBy(NestedObject::getValue,
Collectors.summingInt(NestedObject::getCount)));
Map<String, Long> resultSet = mainObjectList.stream()
.map(MainObject::getNestedList)
.flatMap(List::stream)
.collect(Collectors.groupingBy(NestedObject::getValue,
Collectors.counting()));
如果要按值进行分组
,则计算同一存储桶中的对象数:
Map<String, Integer> resultSet = mainObjectList.stream()
.map(MainObject::getNestedList)
.flatMap(List::stream)
.collect(Collectors.groupingBy(NestedObject::getValue,
Collectors.summingInt(NestedObject::getCount)));
Map<String, Long> resultSet = mainObjectList.stream()
.map(MainObject::getNestedList)
.flatMap(List::stream)
.collect(Collectors.groupingBy(NestedObject::getValue,
Collectors.counting()));
请显示您试图更好地说明您的描述。正确。。。固定在3,2,1..不使用CHRUAIST。。。第二个像葡萄酒一样好喝。非常感谢你,伙计。。。