I have the following data(A class structure):
[
{
"name": "a",
"enable": true
},
{
"name": "a",
"enable": false
},
{
"name": "b",
"enable": false
},
{
"name": "b",
"enable": false
},
{
"name": "c",
"enable": false
}
]
How can I replace the complex code below with a simple steam:
List<A> list = xxxxx;
Map<String, Integer> aCountMap = new HashMap<>();
list.forEach(item -> {
Integer count = aCountMap.get(item.getName());
if (Objects.isNull(count)) {
aCountMap.put(item.getName(), 0);
}
if (item.isEnable()) {
aCountMap.put(item.getName(), ++count);
}
});
The correct result is as follows:
{"a":1}
{"b":0}
{"c":0}