dedupeConsecutive()

Remove consecutive duplicate elements from a stream where equality is measured by Object::equals()

Implementation Notes

This function removes consecutive duplicate elements as measured by Object::equals(). For the purposes of this gatherer, nulls are equal to each other but not equal to anything else.

For a version of dedupeConsecutive() that measures equality with a user-provided function, see the dedupeConsecutiveBy()Gatherer.

Signature

dedupeConsecutive()

Diagram

Marble diagram Marble diagram

Examples

Remove consecutive duplicates

Stream
    .of("A", "A", "A", "B", "B", "C", "C", "D", "A", "B", "C")
    .gather(Gatherers4j.dedupeConsecutive())
    .toList();

// ["A", "B", "C", "D", "A", "B", "C"]

Remove consecutive duplicates, showing treatment of null

Stream
    .of(null, null, "A", "A", null)
    .gather(Gatherers4j.dedupeConsecutive())
    .toList();

// [null, "A", null]