ListResult (@NotNull List<? extends ChildInfo> list) { List<ChildInfo> newChildren = new ArrayList<>(children.size() + list.size()); // assume the list is sorted int index1 = 0; int index2 = 0; while (index1 < children.size() && index2 < list.size()) { ChildInfo element1 = children.get(index1); ChildInfo element2 = list.get(index2); int c = ChildInfo.BY_ID.compare(element1, element2); if (c == 0) { index1++; index2++; } else if (c < 0) { newChildren.add(element1); index1++; } else { // Wow, the element is not among the children. index2++; } } for (int i = index1; i < children.size(); i++) { newChildren.add(children.get(i)); } return new ListResult(parentModStamp, newChildren, parentId); }