liveBook · Manning

Group-by operation

As explained in Data-Oriented Programming in Java

A group-by operation organizes input elements into keyed groups by repeatedly combining values associated with the same key.

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

How Data-Oriented Programming in Java covers it

How deep it goes
Derives groupBy from map combination, binary operations, and stream reduction rather than stopping at a library call.
Examples
Uses duplicate raw records keyed by identifiers and combines each group with a merge operation.
What it assumes
Assumes familiarity with Java lists, maps, loops, functions, and streams while exposing their shared combination pattern.
What you can do after
Build a generic Java groupBy function and adapt its collision behavior through a supplied binary operation.
How it connects
Connects grouping with maps, binary operations, immutable data, streams, reduce, and duplicate-record merging.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming in Java. The section links point to the parts of the book it draws on.