List<LineHits> (@NotNull List<LineHits> aList, @NotNull List<LineHits> bList) { PeekableIterator<LineHits> ai = new PeekableIteratorWrapper<>(aList.iterator()); PeekableIterator<LineHits> bi = new PeekableIteratorWrapper<>(bList.iterator()); List<LineHits> out = new ArrayList<>(); while (ai.hasNext() && bi.hasNext()) { final LineHits x; LineHits a = ai.peek(); LineHits b = bi.peek(); if (a.getLineNumber() < b.getLineNumber()) { x = ai.next(); } else if (a.getLineNumber() > b.getLineNumber()) { x = bi.next(); } else { a.addHits(b.getHits()); x = a; ai.next(); bi.next(); } out.add(x); } addRestItems(out, ai); addRestItems(out, bi); return out; }