Class Iterables
Iterable. Except as noted, each method has a corresponding
Iterator-based method in the Iterators class.
Performance notes: Unless otherwise noted, all of the iterables produced in this class are lazy, which means that their iterators only advance the backing iteration when absolutely necessary.
See the Guava User Guide article on
Iterables.
- Since:
- 2.0 (imported from Google Collections Library)
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> booleanaddAll(Collection<T> addTo, Iterable<? extends T> elementsToAdd) Deprecated.Adds all elements initerabletocollection.static <T> booleanDeprecated.Returnstrueif every element initerablesatisfies the predicate.static <T> booleanDeprecated.Returnstrueif any element initerablesatisfies the predicate.static <T> Iterable<T> Deprecated.Combines multiple iterables into a single iterable.static <T> Iterable<T> Deprecated.Combines multiple iterables into a single iterable.static <T> Iterable<T> Deprecated.Combines two iterables into a single iterable.static <T> Iterable<T> Deprecated.Combines three iterables into a single iterable.static <T> Iterable<T> concat(Iterable<? extends T> a, Iterable<? extends T> b, Iterable<? extends T> c, Iterable<? extends T> d) Deprecated.Combines four iterables into a single iterable.static <T> Iterable<T> consumingIterable(Iterable<T> iterable) Deprecated.Returns a view of the supplied iterable that wraps each generatedIteratorthroughIterators.consumingIterator(Iterator).static booleanDeprecated.Returnstrueifiterablecontains any object for whichequals(element)is true.static <T> Iterable<T> Deprecated.Returns an iterable whose iterators cycle indefinitely over the elements ofiterable.static <T> Iterable<T> cycle(T... elements) Deprecated.Returns an iterable whose iterators cycle indefinitely over the provided elements.static booleanelementsEqual(Iterable<?> iterable1, Iterable<?> iterable2) Deprecated.Determines whether two iterables contain equal elements in the same order.static <T> Iterable<T> Deprecated.Returns all instances of classtypeinunfiltered.static <T> Iterable<T> Deprecated.Returns the elements ofunfilteredthat satisfy a predicate.static <T> TDeprecated.Returns the first element initerablethat satisfies the given predicate, ordefaultValueif none found.static <T> TDeprecated.Returns the first element initerablethat satisfies the given predicate; use this method only when such an element is known to exist.static intDeprecated.Returns the number of elements in the specified iterable that equal the specified object.static <T> TDeprecated.Returns the element at the specified position in an iterable or a default value otherwise.static <T> TDeprecated.Returns the element at the specified position in an iterable.static <T> TDeprecated.Returns the first element initerableordefaultValueif the iterable is empty.static <T> TDeprecated.Returns the last element ofiterableordefaultValueif the iterable is empty.static <T> TDeprecated.Returns the last element ofiterable.static <T> TgetOnlyElement(Iterable<? extends T> iterable, T defaultValue) Deprecated.Returns the single element contained initerable, ordefaultValueif the iterable is empty.static <T> TgetOnlyElement(Iterable<T> iterable) Deprecated.Returns the single element contained initerable.static <T> intDeprecated.Returns the index initerableof the first element that satisfies the providedpredicate, or-1if the Iterable has no such elements.static booleanDeprecated.Determines if the given iterable contains no elements.static <T> Iterable<T> Deprecated.Creates an iterable with the firstlimitSizeelements of the given iterable.static <T> Iterable<T> mergeSorted(Iterable<? extends Iterable<? extends T>> iterables, Comparator<? super T> comparator) Deprecated.Returns an iterable over the merged contents of all giveniterables.paddedPartition(Iterable<T> iterable, int size) Deprecated.Divides an iterable into unmodifiable sublists of the given size, padding the final iterable with null values if necessary.Deprecated.Divides an iterable into unmodifiable sublists of the given size (the final iterable may be smaller).static booleanremoveAll(Iterable<?> removeFrom, Collection<?> elementsToRemove) Deprecated.Removes, from an iterable, every element that belongs to the provided collection.static <T> booleanDeprecated.Removes, from an iterable, every element that satisfies the provided predicate.static booleanretainAll(Iterable<?> removeFrom, Collection<?> elementsToRetain) Deprecated.Removes, from an iterable, every element that does not belong to the provided collection.static intDeprecated.Returns the number of elements initerable.static <T> Iterable<T> Deprecated.Returns a view ofiterablethat skips its firstnumberToSkipelements.static <T> T[]Deprecated.Copies an iterable's elements into an array.static StringDeprecated.Returns a string representation ofiterable, with the format[e1, e2, ..., en].static <F,T> Iterable <T> Deprecated.Returns an iterable that appliesfunctionto each element offromIterable.static <T> Optional<T> Deprecated.Returns anOptionalcontaining the first element initerablethat satisfies the given predicate, if such an element exists.static <E> Iterable<E> unmodifiableIterable(ImmutableCollection<E> iterable) Deprecated.no need to use thisstatic <T> Iterable<T> unmodifiableIterable(Iterable<T> iterable) Deprecated.Returns an unmodifiable view ofiterable.
-
Method Details
-
unmodifiableIterable
Deprecated.Returns an unmodifiable view ofiterable. -
unmodifiableIterable
Deprecated.no need to use thisSimply returns its argument.- Since:
- 10.0
-
size
Deprecated.Returns the number of elements initerable. -
contains
Deprecated.Returnstrueifiterablecontains any object for whichequals(element)is true. -
removeAll
Deprecated.Removes, from an iterable, every element that belongs to the provided collection.This method calls
Collection.removeAll(java.util.Collection<?>)ifiterableis a collection, andIterators.removeAll(java.util.Iterator<?>, java.util.Collection<?>)otherwise.- Parameters:
removeFrom- the iterable to (potentially) remove elements fromelementsToRemove- the elements to remove- Returns:
trueif any element was removed fromiterable
-
retainAll
Deprecated.Removes, from an iterable, every element that does not belong to the provided collection.This method calls
Collection.retainAll(java.util.Collection<?>)ifiterableis a collection, andIterators.retainAll(java.util.Iterator<?>, java.util.Collection<?>)otherwise.- Parameters:
removeFrom- the iterable to (potentially) remove elements fromelementsToRetain- the elements to retain- Returns:
trueif any element was removed fromiterable
-
removeIf
Deprecated.Removes, from an iterable, every element that satisfies the provided predicate.- Parameters:
removeFrom- the iterable to (potentially) remove elements frompredicate- a predicate that determines whether an element should be removed- Returns:
trueif any elements were removed from the iterable- Throws:
UnsupportedOperationException- if the iterable does not supportremove().- Since:
- 2.0
-
elementsEqual
Deprecated.Determines whether two iterables contain equal elements in the same order. More specifically, this method returnstrueifiterable1anditerable2contain the same number of elements and every element ofiterable1is equal to the corresponding element ofiterable2. -
toString
Deprecated.Returns a string representation ofiterable, with the format[e1, e2, ..., en]. -
getOnlyElement
Deprecated.Returns the single element contained initerable.- Throws:
NoSuchElementException- if the iterable is emptyIllegalArgumentException- if the iterable contains multiple elements
-
getOnlyElement
@Nullable public static <T> T getOnlyElement(Iterable<? extends T> iterable, @Nullable T defaultValue) Deprecated.Returns the single element contained initerable, ordefaultValueif the iterable is empty.- Throws:
IllegalArgumentException- if the iterator contains multiple elements
-
toArray
@GwtIncompatible("Array.newInstance(Class, int)") public static <T> T[] toArray(Iterable<? extends T> iterable, Class<T> type) Deprecated.Copies an iterable's elements into an array.- Parameters:
iterable- the iterable to copytype- the type of the elements- Returns:
- a newly-allocated array into which all the elements of the iterable have been copied
-
addAll
Deprecated.Adds all elements initerabletocollection.- Returns:
trueifcollectionwas modified as a result of this operation.
-
frequency
Deprecated.Returns the number of elements in the specified iterable that equal the specified object. This implementation avoids a full iteration when the iterable is aMultisetorSet.- See Also:
-
cycle
Deprecated.Returns an iterable whose iterators cycle indefinitely over the elements ofiterable.That iterator supports
remove()ifiterable.iterator()does. Afterremove()is called, subsequent cycles omit the removed element, which is no longer initerable. The iterator'shasNext()method returnstrueuntiliterableis empty.Warning: Typical uses of the resulting iterator may produce an infinite loop. You should use an explicit
breakor be certain that you will eventually remove all the elements.To cycle over the iterable
ntimes, use the following:Iterables.concat(Collections.nCopies(n, iterable)) -
cycle
Deprecated.Returns an iterable whose iterators cycle indefinitely over the provided elements.After
removeis invoked on a generated iterator, the removed element will no longer appear in either that iterator or any other iterator created from the same source iterable. That is, this method behaves exactly asIterables.cycle(Lists.newArrayList(elements)). The iterator'shasNextmethod returnstrueuntil all of the original elements have been removed.Warning: Typical uses of the resulting iterator may produce an infinite loop. You should use an explicit
breakor be certain that you will eventually remove all the elements.To cycle over the elements
ntimes, use the following:Iterables.concat(Collections.nCopies(n, Arrays.asList(elements))) -
concat
Deprecated.Combines two iterables into a single iterable. The returned iterable has an iterator that traverses the elements ina, followed by the elements inb. The source iterators are not polled until necessary.The returned iterable's iterator supports
remove()when the corresponding input iterator supports it. -
concat
public static <T> Iterable<T> concat(Iterable<? extends T> a, Iterable<? extends T> b, Iterable<? extends T> c) Deprecated.Combines three iterables into a single iterable. The returned iterable has an iterator that traverses the elements ina, followed by the elements inb, followed by the elements inc. The source iterators are not polled until necessary.The returned iterable's iterator supports
remove()when the corresponding input iterator supports it. -
concat
public static <T> Iterable<T> concat(Iterable<? extends T> a, Iterable<? extends T> b, Iterable<? extends T> c, Iterable<? extends T> d) Deprecated.Combines four iterables into a single iterable. The returned iterable has an iterator that traverses the elements ina, followed by the elements inb, followed by the elements inc, followed by the elements ind. The source iterators are not polled until necessary.The returned iterable's iterator supports
remove()when the corresponding input iterator supports it. -
concat
Deprecated.Combines multiple iterables into a single iterable. The returned iterable has an iterator that traverses the elements of each iterable ininputs. The input iterators are not polled until necessary.The returned iterable's iterator supports
remove()when the corresponding input iterator supports it.- Throws:
NullPointerException- if any of the provided iterables is null
-
concat
Deprecated.Combines multiple iterables into a single iterable. The returned iterable has an iterator that traverses the elements of each iterable ininputs. The input iterators are not polled until necessary.The returned iterable's iterator supports
remove()when the corresponding input iterator supports it. The methods of the returned iterable may throwNullPointerExceptionif any of the input iterators is null. -
partition
Deprecated.Divides an iterable into unmodifiable sublists of the given size (the final iterable may be smaller). For example, partitioning an iterable containing[a, b, c, d, e]with a partition size of 3 yields[[a, b, c], [d, e]]-- an outer iterable containing two inner lists of three and two elements, all in the original order.Iterators returned by the returned iterable do not support the
Iterator.remove()method. The returned lists implementRandomAccess, whether or not the input list does.Note: if
iterableis aList, useLists.partition(List, int)instead.- Parameters:
iterable- the iterable to return a partitioned view ofsize- the desired size of each partition (the last may be smaller)- Returns:
- an iterable of unmodifiable lists containing the elements of
iterabledivided into partitions - Throws:
IllegalArgumentException- ifsizeis nonpositive
-
paddedPartition
Deprecated.Divides an iterable into unmodifiable sublists of the given size, padding the final iterable with null values if necessary. For example, partitioning an iterable containing[a, b, c, d, e]with a partition size of 3 yields[[a, b, c], [d, e, null]]-- an outer iterable containing two inner lists of three elements each, all in the original order.Iterators returned by the returned iterable do not support the
Iterator.remove()method.- Parameters:
iterable- the iterable to return a partitioned view ofsize- the desired size of each partition- Returns:
- an iterable of unmodifiable lists containing the elements of
iterabledivided into partitions (the final iterable may have trailing null elements) - Throws:
IllegalArgumentException- ifsizeis nonpositive
-
filter
Deprecated.Returns the elements ofunfilteredthat satisfy a predicate. The resulting iterable's iterator does not supportremove(). -
filter
@GwtIncompatible("Class.isInstance") public static <T> Iterable<T> filter(Iterable<?> unfiltered, Class<T> type) Deprecated.Returns all instances of classtypeinunfiltered. The returned iterable has elements whose class istypeor a subclass oftype. The returned iterable's iterator does not supportremove().- Parameters:
unfiltered- an iterable containing objects of any typetype- the type of elements desired- Returns:
- an unmodifiable iterable containing all elements of the original iterable that were of the requested type
-
any
Deprecated.Returnstrueif any element initerablesatisfies the predicate. -
all
Deprecated.Returnstrueif every element initerablesatisfies the predicate. Ifiterableis empty,trueis returned. -
find
Deprecated.Returns the first element initerablethat satisfies the given predicate; use this method only when such an element is known to exist. If it is possible that no element will match, usetryFind(java.lang.Iterable<T>, com.google.common.base.Predicate<? super T>)orfind(Iterable, Predicate, Object)instead.- Throws:
NoSuchElementException- if no element initerablematches the given predicate
-
find
@Nullable public static <T> T find(Iterable<? extends T> iterable, Predicate<? super T> predicate, @Nullable T defaultValue) Deprecated.Returns the first element initerablethat satisfies the given predicate, ordefaultValueif none found. Note that this can usually be handled more naturally usingtryFind(iterable, predicate).or(defaultValue).- Since:
- 7.0
-
tryFind
Deprecated.Returns anOptionalcontaining the first element initerablethat satisfies the given predicate, if such an element exists.Warning: avoid using a
predicatethat matchesnull. Ifnullis matched initerable, a NullPointerException will be thrown.- Since:
- 11.0
-
indexOf
Deprecated.Returns the index initerableof the first element that satisfies the providedpredicate, or-1if the Iterable has no such elements.More formally, returns the lowest index
isuch thatpredicate.apply(Iterables.get(iterable, i))returnstrue, or-1if there is no such index.- Since:
- 2.0
-
transform
public static <F,T> Iterable<T> transform(Iterable<F> fromIterable, Function<? super F, ? extends T> function) Deprecated.Returns an iterable that appliesfunctionto each element offromIterable.The returned iterable's iterator supports
remove()if the provided iterator does. After a successfulremove()call,fromIterableno longer contains the corresponding element.If the input
Iterableis known to be aListor otherCollection, considerLists.transform(java.util.List<F>, com.google.common.base.Function<? super F, ? extends T>)andCollections2.transform(java.util.Collection<F>, com.google.common.base.Function<? super F, T>). -
get
Deprecated.Returns the element at the specified position in an iterable.- Parameters:
position- position of the element to return- Returns:
- the element at the specified position in
iterable - Throws:
IndexOutOfBoundsException- ifpositionis negative or greater than or equal to the size ofiterable
-
get
@Nullable public static <T> T get(Iterable<? extends T> iterable, int position, @Nullable T defaultValue) Deprecated.Returns the element at the specified position in an iterable or a default value otherwise.- Parameters:
position- position of the element to returndefaultValue- the default value to return ifpositionis greater than or equal to the size of the iterable- Returns:
- the element at the specified position in
iterableordefaultValueifiterablecontains fewer thanposition + 1elements. - Throws:
IndexOutOfBoundsException- ifpositionis negative- Since:
- 4.0
-
getFirst
Deprecated.Returns the first element initerableordefaultValueif the iterable is empty. TheIteratorsanalog to this method isIterators.getNext(java.util.Iterator<? extends T>, T).If no default value is desired (and the caller instead wants a
NoSuchElementExceptionto be thrown), it is recommended thatiterable.iterator().next()is used instead.- Parameters:
defaultValue- the default value to return if the iterable is empty- Returns:
- the first element of
iterableor the default value - Since:
- 7.0
-
getLast
Deprecated.Returns the last element ofiterable.- Returns:
- the last element of
iterable - Throws:
NoSuchElementException- if the iterable is empty
-
getLast
Deprecated.Returns the last element ofiterableordefaultValueif the iterable is empty.- Parameters:
defaultValue- the value to return ifiterableis empty- Returns:
- the last element of
iterableor the default value - Since:
- 3.0
-
skip
Deprecated.Returns a view ofiterablethat skips its firstnumberToSkipelements. Ifiterablecontains fewer thannumberToSkipelements, the returned iterable skips all of its elements.Modifications to the underlying
Iterablebefore a call toiterator()are reflected in the returned iterator. That is, the iterator skips the firstnumberToSkipelements that exist when theIteratoris created, not whenskip()is called.The returned iterable's iterator supports
remove()if the iterator of the underlying iterable supports it. Note that it is not possible to delete the last skipped element by immediately callingremove()on that iterator, as theIteratorcontract states that a call toremove()before a call tonext()will throw anIllegalStateException.- Since:
- 3.0
-
limit
Deprecated.Creates an iterable with the firstlimitSizeelements of the given iterable. If the original iterable does not contain that many elements, the returned iterator will have the same behavior as the original iterable. The returned iterable's iterator supportsremove()if the original iterator does.- Parameters:
iterable- the iterable to limitlimitSize- the maximum number of elements in the returned iterator- Throws:
IllegalArgumentException- iflimitSizeis negative- Since:
- 3.0
-
consumingIterable
Deprecated.Returns a view of the supplied iterable that wraps each generatedIteratorthroughIterators.consumingIterator(Iterator).Note: If
iterableis aQueue, the returned iterable will get entries fromQueue.remove()sinceQueue's iteration order is undefined. CallingIterator.hasNext()on a generated iterator from the returned iterable may cause an item to be immediately dequeued for return on a subsequent call toIterator.next().- Parameters:
iterable- the iterable to wrap- Returns:
- a view of the supplied iterable that wraps each generated iterator
through
Iterators.consumingIterator(Iterator); for queues, an iterable that generates iterators that return and consume the queue's elements in queue order - Since:
- 2.0
- See Also:
-
isEmpty
Deprecated.Determines if the given iterable contains no elements.There is no precise
Iteratorequivalent to this method, since one can only ask an iterator whether it has any elements remaining (which one does usingIterator.hasNext()).- Returns:
trueif the iterable contains no elements
-
mergeSorted
@Beta public static <T> Iterable<T> mergeSorted(Iterable<? extends Iterable<? extends T>> iterables, Comparator<? super T> comparator) Deprecated.Returns an iterable over the merged contents of all giveniterables. Equivalent entries will not be de-duplicated.Callers must ensure that the source
iterablesare in non-descending order as this method does not sort its input.For any equivalent elements across all
iterables, it is undefined which element is returned first.- Since:
- 11.0
-