/** * A sensible definition of {@link #addAll} in terms of {@link #add}. If you * override {@link #add}, you may wish to override {@link #addAll} to forward * to this implementation. * * @since 7.0 */ protected boolean standardAddAll(Collection<? extends E> collection) { return Iterators.addAll(this, collection.iterator()); }
private static <E> ArrayList<E> toArrayList(Collection<E> c) { // Avoid calling ArrayList(Collection), which may call back into toArray. ArrayList<E> result = new ArrayList<E>(c.size()); Iterators.addAll(result, c.iterator()); return result; }
private static <E> ArrayList<E> toArrayList(Collection<E> c) { // Avoid calling ArrayList(Collection), which may call back into toArray. ArrayList<E> result = new ArrayList<E>(c.size()); Iterators.addAll(result, c.iterator()); return result; }
/** * Creates a <i>mutable</i> {@code HashSet} instance containing the given elements. A very thin * convenience for creating an empty set and then calling {@link Iterators#addAll}. * * <p><b>Note:</b> if mutability is not required and the elements are non-null, use {@link * ImmutableSet#copyOf(Iterator)} instead. * * <p><b>Note:</b> if {@code E} is an {@link Enum} type, you should create an {@link EnumSet} * instead. * * <p>Overall, this method is not very useful and will likely be deprecated in the future. */ public static <E> HashSet<E> newHashSet(Iterator<? extends E> elements) { HashSet<E> set = newHashSet(); Iterators.addAll(set, elements); return set; }
@CanIgnoreReturnValue @Override public boolean putAll(@Nullable K key, Iterable<? extends V> values) { checkNotNull(values); // make sure we only call values.iterator() once // and we only call get(key) if values is nonempty if (values instanceof Collection) { Collection<? extends V> valueCollection = (Collection<? extends V>) values; return !valueCollection.isEmpty() && get(key).addAll(valueCollection); } else { Iterator<? extends V> valueItr = values.iterator(); return valueItr.hasNext() && Iterators.addAll(get(key), valueItr); } }
/** * Adds all elements in {@code iterable} to {@code collection}. * * @return {@code true} if {@code collection} was modified as a result of this * operation. */ @CanIgnoreReturnValue public static <T> boolean addAll(Collection<T> addTo, Iterable<? extends T> elementsToAdd) { if (elementsToAdd instanceof Collection) { Collection<? extends T> c = Collections2.cast(elementsToAdd); return addTo.addAll(c); } return Iterators.addAll(addTo, checkNotNull(elementsToAdd).iterator()); }
/** * Returns an immutable multiset containing the given elements, in the "grouped iteration order" * described in the class documentation. * * @throws NullPointerException if any of {@code elements} is null */ public static <E> ImmutableMultiset<E> copyOf(Iterator<? extends E> elements) { Multiset<E> multiset = LinkedHashMultiset.create(); Iterators.addAll(multiset, elements); return copyFromEntries(multiset.entrySet()); }
/** * Creates a <i>mutable</i> {@code ArrayList} instance containing the given * elements; a very thin shortcut for creating an empty list and then calling * {@link Iterators#addAll}. * * <p><b>Note:</b> if mutability is not required and the elements are * non-null, use {@link ImmutableList#copyOf(Iterator)} instead. */ @CanIgnoreReturnValue // TODO(kak): Remove this @GwtCompatible(serializable = true) public static <E> ArrayList<E> newArrayList(Iterator<? extends E> elements) { ArrayList<E> list = newArrayList(); Iterators.addAll(list, elements); return list; }
if (itr.hasNext()) { EnumSet<E> enumSet = EnumSet.of(itr.next()); Iterators.addAll(enumSet, itr); return ImmutableEnumSet.asImmutable(enumSet); } else {
/** * An implementation of {@link Multiset#addAll}. */ static <E> boolean addAllImpl(Multiset<E> self, Collection<? extends E> elements) { if (elements.isEmpty()) { return false; } if (elements instanceof Multiset) { Multiset<? extends E> that = cast(elements); for (Entry<? extends E> entry : that.entrySet()) { self.add(entry.getElement(), entry.getCount()); } } else { Iterators.addAll(self, elements.iterator()); } return true; }