/** * Creates an {@code ArrayDeque} containing the elements of the specified iterable, in the order * they are returned by the iterable's iterator. * * @since 12.0 */ public static <E> ArrayDeque<E> newArrayDeque(Iterable<? extends E> elements) { if (elements instanceof Collection) { return new ArrayDeque<E>(Collections2.cast(elements)); } ArrayDeque<E> deque = new ArrayDeque<E>(); Iterables.addAll(deque, elements); return deque; }
/** * Creates a {@code PriorityQueue} containing the given elements. * * <p><b>Note:</b> If the specified iterable is a {@code SortedSet} or a {@code PriorityQueue}, * this priority queue will be ordered according to the same ordering. * * @since 11.0 (requires that {@code E} be {@code Comparable} since 15.0). */ public static <E extends Comparable> PriorityQueue<E> newPriorityQueue( Iterable<? extends E> elements) { if (elements instanceof Collection) { return new PriorityQueue<E>(Collections2.cast(elements)); } PriorityQueue<E> queue = new PriorityQueue<E>(); Iterables.addAll(queue, elements); return queue; }
/** * Creates a <i>mutable</i> {@code LinkedHashSet} instance containing the given elements in order. * * <p><b>Note:</b> if mutability is not required and the elements are non-null, use {@link * ImmutableSet#copyOf(Iterable)} instead. * * <p><b>Note for Java 7 and later:</b> if {@code elements} is a {@link Collection}, you don't * need this method. Instead, use the {@code LinkedHashSet} constructor directly, taking advantage * of the new <a href="http://goo.gl/iz2Wi">"diamond" syntax</a>. * * <p>Overall, this method is not very useful and will likely be deprecated in the future. * * @param elements the elements that the set should contain, in order * @return a new {@code LinkedHashSet} containing those elements (minus duplicates) */ public static <E> LinkedHashSet<E> newLinkedHashSet(Iterable<? extends E> elements) { if (elements instanceof Collection) { return new LinkedHashSet<E>(Collections2.cast(elements)); } LinkedHashSet<E> set = newLinkedHashSet(); Iterables.addAll(set, elements); return set; }
/** * Creates a <i>mutable</i> {@code HashSet} instance containing the given elements. A very thin * convenience for creating an empty set then calling {@link Collection#addAll} or {@link * Iterables#addAll}. * * <p><b>Note:</b> if mutability is not required and the elements are non-null, use {@link * ImmutableSet#copyOf(Iterable)} instead. (Or, change {@code elements} to be a {@link * FluentIterable} and call {@code elements.toSet()}.) * * <p><b>Note:</b> if {@code E} is an {@link Enum} type, use {@link #newEnumSet(Iterable, Class)} * instead. * * <p><b>Note for Java 7 and later:</b> if {@code elements} is a {@link Collection}, you don't * need this method. Instead, use the {@code HashSet} constructor directly, taking advantage of * the new <a href="http://goo.gl/iz2Wi">"diamond" syntax</a>. * * <p>Overall, this method is not very useful and will likely be deprecated in the future. */ public static <E> HashSet<E> newHashSet(Iterable<? extends E> elements) { return (elements instanceof Collection) ? new HashSet<E>(Collections2.cast(elements)) : newHashSet(elements.iterator()); }
/** * Creates a {@code ConcurrentLinkedQueue} containing the elements of the specified iterable, in * the order they are returned by the iterable's iterator. */ @GwtIncompatible // ConcurrentLinkedQueue public static <E> ConcurrentLinkedQueue<E> newConcurrentLinkedQueue( Iterable<? extends E> elements) { if (elements instanceof Collection) { return new ConcurrentLinkedQueue<E>(Collections2.cast(elements)); } ConcurrentLinkedQueue<E> queue = new ConcurrentLinkedQueue<E>(); Iterables.addAll(queue, elements); return queue; }
/** * Creates a {@code CopyOnWriteArrayList} instance containing the given elements. * * @param elements the elements that the list should contain, in order * @return a new {@code CopyOnWriteArrayList} containing those elements * @since 12.0 */ @GwtIncompatible // CopyOnWriteArrayList public static <E> CopyOnWriteArrayList<E> newCopyOnWriteArrayList( Iterable<? extends E> elements) { // We copy elements to an ArrayList first, rather than incurring the // quadratic cost of adding them to the COWAL directly. Collection<? extends E> elementsCollection = (elements instanceof Collection) ? Collections2.cast(elements) : newArrayList(elements); return new CopyOnWriteArrayList<>(elementsCollection); }
/** * Creates a {@code LinkedBlockingDeque} with a capacity of {@link Integer#MAX_VALUE}, containing * the elements of the specified iterable, in the order they are returned by the iterable's * iterator. * * @since 12.0 */ @GwtIncompatible // LinkedBlockingDeque public static <E> LinkedBlockingDeque<E> newLinkedBlockingDeque(Iterable<? extends E> elements) { if (elements instanceof Collection) { return new LinkedBlockingDeque<E>(Collections2.cast(elements)); } LinkedBlockingDeque<E> deque = new LinkedBlockingDeque<E>(); Iterables.addAll(deque, elements); return deque; }
/** * Creates a {@code LinkedBlockingQueue} with a capacity of {@link Integer#MAX_VALUE}, containing * the elements of the specified iterable, in the order they are returned by the iterable's * iterator. * * @param elements the elements that the queue should contain, in order * @return a new {@code LinkedBlockingQueue} containing those elements */ @GwtIncompatible // LinkedBlockingQueue public static <E> LinkedBlockingQueue<E> newLinkedBlockingQueue(Iterable<? extends E> elements) { if (elements instanceof Collection) { return new LinkedBlockingQueue<E>(Collections2.cast(elements)); } LinkedBlockingQueue<E> queue = new LinkedBlockingQueue<E>(); Iterables.addAll(queue, elements); return queue; }
/** * Creates a {@code CopyOnWriteArraySet} instance containing the given elements. * * @param elements the elements that the set should contain, in order * @return a new {@code CopyOnWriteArraySet} containing those elements * @since 12.0 */ @GwtIncompatible // CopyOnWriteArraySet public static <E> CopyOnWriteArraySet<E> newCopyOnWriteArraySet(Iterable<? extends E> elements) { // We copy elements to an ArrayList first, rather than incurring the // quadratic cost of adding them to the COWAS directly. Collection<? extends E> elementsCollection = (elements instanceof Collection) ? Collections2.cast(elements) : Lists.newArrayList(elements); return new CopyOnWriteArraySet<E>(elementsCollection); }
/** * Creates a {@code PriorityBlockingQueue} containing the given elements. * * <p><b>Note:</b> If the specified iterable is a {@code SortedSet} or a {@code PriorityQueue}, * this priority queue will be ordered according to the same ordering. * * @since 11.0 (requires that {@code E} be {@code Comparable} since 15.0). */ @GwtIncompatible // PriorityBlockingQueue public static <E extends Comparable> PriorityBlockingQueue<E> newPriorityBlockingQueue( Iterable<? extends E> elements) { if (elements instanceof Collection) { return new PriorityBlockingQueue<E>(Collections2.cast(elements)); } PriorityBlockingQueue<E> queue = new PriorityBlockingQueue<E>(); Iterables.addAll(queue, elements); return queue; }
/** * Creates an {@code ArrayDeque} containing the elements of the specified iterable, in the order * they are returned by the iterable's iterator. * * @since 12.0 */ public static <E> ArrayDeque<E> newArrayDeque(Iterable<? extends E> elements) { if (elements instanceof Collection) { return new ArrayDeque<E>(Collections2.cast(elements)); } ArrayDeque<E> deque = new ArrayDeque<E>(); Iterables.addAll(deque, elements); return deque; }
/** * 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()); }
/** * Creates a {@code PriorityQueue} containing the given elements. * * <p><b>Note:</b> If the specified iterable is a {@code SortedSet} or a {@code PriorityQueue}, * this priority queue will be ordered according to the same ordering. * * @since 11.0 (requires that {@code E} be {@code Comparable} since 15.0). */ public static <E extends Comparable> PriorityQueue<E> newPriorityQueue( Iterable<? extends E> elements) { if (elements instanceof Collection) { return new PriorityQueue<E>(Collections2.cast(elements)); } PriorityQueue<E> queue = new PriorityQueue<E>(); Iterables.addAll(queue, elements); return queue; }
/** * Creates a {@code ConcurrentLinkedQueue} containing the elements of the specified iterable, in * the order they are returned by the iterable's iterator. */ @GwtIncompatible // ConcurrentLinkedQueue public static <E> ConcurrentLinkedQueue<E> newConcurrentLinkedQueue( Iterable<? extends E> elements) { if (elements instanceof Collection) { return new ConcurrentLinkedQueue<E>(Collections2.cast(elements)); } ConcurrentLinkedQueue<E> queue = new ConcurrentLinkedQueue<E>(); Iterables.addAll(queue, elements); return queue; }
/** * Creates a <i>mutable</i> {@code ArrayList} instance containing the given elements; a very thin * shortcut for creating an empty list then calling {@link Iterables#addAll}. * * <p><b>Note:</b> if mutability is not required and the elements are non-null, use {@link * ImmutableList#copyOf(Iterable)} instead. (Or, change {@code elements} to be a {@link * FluentIterable} and call {@code elements.toList()}.) * * <p><b>Note for Java 7 and later:</b> if {@code elements} is a {@link Collection}, you don't * need this method. Use the {@code ArrayList} {@linkplain ArrayList#ArrayList(Collection) * constructor} directly, taking advantage of the new <a href="http://goo.gl/iz2Wi">"diamond" * syntax</a>. */ @CanIgnoreReturnValue // TODO(kak): Remove this @GwtCompatible(serializable = true) public static <E> ArrayList<E> newArrayList(Iterable<? extends E> elements) { checkNotNull(elements); // for GWT // Let ArrayList's sizing logic work, if possible return (elements instanceof Collection) ? new ArrayList<>(Collections2.cast(elements)) : newArrayList(elements.iterator()); }
/** * Copies all the elements from this fluent iterable to {@code collection}. This is equivalent to * calling {@code Iterables.addAll(collection, this)}. * * <p><b>{@code Stream} equivalent:</b> {@code stream.forEachOrdered(collection::add)} or {@code * stream.forEach(collection::add)}. * * @param collection the collection to copy elements to * @return {@code collection}, for convenience * @since 14.0 */ @CanIgnoreReturnValue public final <C extends Collection<? super E>> C copyInto(C collection) { checkNotNull(collection); Iterable<E> iterable = getDelegate(); if (iterable instanceof Collection) { collection.addAll(Collections2.cast(iterable)); } else { for (E item : iterable) { collection.add(item); } } return collection; }
/** * Creates a {@code LinkedBlockingDeque} with a capacity of {@link Integer#MAX_VALUE}, containing * the elements of the specified iterable, in the order they are returned by the iterable's * iterator. * * @since 12.0 */ @GwtIncompatible // LinkedBlockingDeque public static <E> LinkedBlockingDeque<E> newLinkedBlockingDeque(Iterable<? extends E> elements) { if (elements instanceof Collection) { return new LinkedBlockingDeque<E>(Collections2.cast(elements)); } LinkedBlockingDeque<E> deque = new LinkedBlockingDeque<E>(); Iterables.addAll(deque, elements); return deque; }
/** * Creates a {@code CopyOnWriteArrayList} instance containing the given elements. * * @param elements the elements that the list should contain, in order * @return a new {@code CopyOnWriteArrayList} containing those elements * @since 12.0 */ @GwtIncompatible // CopyOnWriteArrayList public static <E> CopyOnWriteArrayList<E> newCopyOnWriteArrayList( Iterable<? extends E> elements) { // We copy elements to an ArrayList first, rather than incurring the // quadratic cost of adding them to the COWAL directly. Collection<? extends E> elementsCollection = (elements instanceof Collection) ? Collections2.cast(elements) : newArrayList(elements); return new CopyOnWriteArrayList<>(elementsCollection); }
/** * Creates a {@code LinkedBlockingQueue} with a capacity of {@link Integer#MAX_VALUE}, containing * the elements of the specified iterable, in the order they are returned by the iterable's * iterator. * * @param elements the elements that the queue should contain, in order * @return a new {@code LinkedBlockingQueue} containing those elements */ @GwtIncompatible // LinkedBlockingQueue public static <E> LinkedBlockingQueue<E> newLinkedBlockingQueue(Iterable<? extends E> elements) { if (elements instanceof Collection) { return new LinkedBlockingQueue<E>(Collections2.cast(elements)); } LinkedBlockingQueue<E> queue = new LinkedBlockingQueue<E>(); Iterables.addAll(queue, elements); return queue; }
/** * Returns the last element of {@code iterable} or {@code defaultValue} if the iterable is empty. * If {@code iterable} is a {@link List} with {@link RandomAccess} support, then this operation is * guaranteed to be {@code O(1)}. * * <p><b>{@code Stream} equivalent:</b> {@code Streams.findLast(stream).orElse(defaultValue)} * * @param defaultValue the value to return if {@code iterable} is empty * @return the last element of {@code iterable} or the default value * @since 3.0 */ public static <T> @Nullable T getLast(Iterable<? extends T> iterable, @Nullable T defaultValue) { if (iterable instanceof Collection) { Collection<? extends T> c = Collections2.cast(iterable); if (c.isEmpty()) { return defaultValue; } else if (iterable instanceof List) { return getLastInNonemptyList(Lists.cast(iterable)); } } return Iterators.getLast(iterable.iterator(), defaultValue); }