List<DfaMemoryState> (List<DfaMemoryState> states) { if (states.size() < FORCE_MERGE_THRESHOLD) return states; myWasForciblyMerged = true; Collection<List<DfaMemoryState>> groups = StreamEx.of(states).groupingBy(DfaMemoryState::getMergeabilityKey).values(); return StreamEx.of(groups) .flatMap(group -> StreamEx.ofSubLists(group, 2) .map(pair -> { if (pair.size() == 2) { pair.get(0).merge(pair.get(1)); } return pair.get(0); })).distinct().toListAndThen(StateQueue::squash); }