int (@NotNull Collection<? extends E> c) { // optimization: faster than calling .add() one by one if (c.isEmpty()) { return 0; } Object[] elements; Object[] newElements; int added; do { elements = get(); Set<Object> existing = ContainerUtil.map2Set(elements, Functions.identity()); List<Object> toAddList = new ArrayList<>(c.size()); for (E e : c) { if (!existing.contains(e)) toAddList.add(e); } added = toAddList.size(); if (added == 0) { return 0; } newElements = ArrayUtil.mergeArrays(elements, toAddList.toArray()); } while (!replaceArray(elements, newElements)); return added; }