ListResult (@NotNull ChildInfo child) { List<ChildInfo> newChildren = new ArrayList<>(children.size() + 1); int id = child.getId(); int i = ObjectUtils.binarySearch(0, children.size(), mid -> Integer.compare(children.get(mid).getId(), id)); if (i >= 0) { // wow, child already there, replace array with the new child newChildren.addAll(children); newChildren.set(i, child); } else { int toInsert = -i - 1; for (int j = 0; j < toInsert; j++) { newChildren.add(children.get(j)); } newChildren.add(child); for (int j = toInsert; j < children.size(); j++) { newChildren.add(children.get(j)); } } return new ListResult(parentModStamp, newChildren, parentId); }