I need to implement a pivot table in Java and I know how to do with Java 8 Streams features. There are a lot of good solution over the web but I need something more and I don't understand how to do that: I need to create a more dynamic table where ideally you don't know for what columns you have to aggregate. For example, if I have the columns ("Nation", "Company", "Industry","Number of employes") I have to give as input:
- A custom aggregation function (ex. sum) for the measure
- A variable order of aggregation: ex., I want first aggregate for Nation and I gave as argument "Nation" or for Nation and Company and I gave as argument something like "Nation->Company". In other words, I don't know which are the fields for my aggregation and basically I need a way to implement a generic GROUP BY SQL clause, so something like:
// Given an the Arraylist ("Nation", "Company", "Industry","Number of employes") called data with some rows
Map<String, List<Object[]>> map = data.stream().collect(
Collectors.groupingBy(row -> row[0].toString() + "-" + row[1].toString()));
for (Map.Entry<String, List<Object[]>> entry : map.entrySet()) {
final double average = entry.getValue().stream()
.mapToInt(row -> (int) row[3]).average().getAsDouble();
It's not what I need because it is too explicit.
I need to:
- Split the input Arraylist in sublist by value given by the header name which I extract from my data (or more, it depends for how many column I have to group by)
- Aggregate each sublist
- Union the sublist
Could someone help or boost me? Thanks