A group-by operation organizes input elements into keyed groups by repeatedly combining values associated with the same key. In Java, a classifier supplies each key while the implementation accumulates values in a map. section 7.3
A data-oriented implementation makes the operation explicit:
- Transform each item into a one-entry map containing its classified key and value. section 7.3
- Combine maps with a binary operation that resolves collisions by combining values rather than discarding them. section 7.3
- Express the same process with streams and reduce when that pipeline clarifies the transformation. section 7.3
Cleaning duplicate records
Classify raw records with RawData::id, then merge each group. The resulting control logic separates grouping from duplicate resolution and can ultimately reduce to a map of merged records. section 7.3
Listing 7.7 Control logic for cleaning the duplicate records
static List<RawData> cleanDuplicates(List<RawData> rows) { #A
Map<String, List<RawData>> dupesById = new HashMap<>(); #B
for (RawData row : rows) {
String key = row.id();
if (!dupesById.containsKey(key)) {
dupesById.put(key, new ArrayList<>());
}
dupesById.get(key).add(row);
}
List<RawData> cleaned = new ArrayList<>(); #C
for (List<RawData> duplicates : dupesById.values()) { #D
RawData merged = duplicates.getFirst();
for (RawData other : duplicates.subList(1, duplicates.size())) { #E
merged = add(merged, other); #E
}
cleaned.add(merged); #F
}
return cleaned;
}Compare the explicit loops, map state, and repeated merge step that motivate extracting groupBy. section 7.3
