I have 2 issues that I can't seem to solve. The first one is I need a way to have dynamic nested grouping by where there could be 1-n nested groups that the user can pass in.
The second issue is that I need the results to be flatten where the keys are concat rather than nested.
My example input looks like this:
List<Map<String, String>> fakeData = new LinkedList<>();
Map<String, String> data1 = new HashMap<>();
data1.put("ip","10.0.1.0");
data1.put("uid","root");
data1.put("group","admin");
fakeData.add(data1);
Map<String, String> data2 = new HashMap<>();
data2.put("ip","10.0.1.1");
data2.put("uid","tiger");
data2.put("group","user");
fakeData.add(data2);
Map<String, String> data3 = new HashMap<>();
data3.put("ip","10.0.1.1");
data3.put("uid","woods");
data3.put("group","user");
fakeData.add(data3);
The end result have a concat of map keys:
{
"10.0.1.1user": [
{
"uid": "tiger",
"ip": "10.0.1.1",
"group": "user"
},
{
"uid": "woods",
"ip": "10.0.1.1",
"group": "user"
}
],
"10.0.1.0admin": [
"uid": "root",
"ip": "10.0.1.0",
"group": "admin"
]
}
Notice the keys are concat rather than nested maps within maps.
I'm trying to create a groupingby where it can be dynamic without any luck:
fakeData.stream()
.collect(groupingBy(map -> map.get("ip"),
groupingBy(map -> map.get("uuid"),
... nested "n" times)));
This is the interface that I'm trying to implement:
public Map<String, List<Map<String, String>>> doGrouping(List<String> columns,
List<Map<String, String>> data);
doGroupingwith your example data, and what the expected output would be?columnsargument ofdoGroupingcontainsNelements, then you need to create nested groups by each one of itsNelements? And that you want the key of each group to be concatenated rather than nested?groupingBy(map -> map.get("ip") + map.get("uuid"))work? For a list of keys, maybelistOfKeys.stream().map(map::get).collect(Collectors.joining()).