Class Collections2
- java.lang.Object
-
- com.google.common.collect.Collections2
-
@GwtCompatible public final class Collections2 extends java.lang.Object
Provides static methods for working withCollection
instances.- Since:
- 2.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
Collections2.FilteredCollection<E>
private static class
Collections2.OrderedPermutationCollection<E>
private static class
Collections2.OrderedPermutationIterator<E>
private static class
Collections2.PermutationCollection<E>
private static class
Collections2.PermutationIterator<E>
(package private) static class
Collections2.TransformedCollection<F,T>
-
Field Summary
Fields Modifier and Type Field Description (package private) static Joiner
STANDARD_JOINER
-
Constructor Summary
Constructors Modifier Constructor Description private
Collections2()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description (package private) static <T> java.util.Collection<T>
cast(java.lang.Iterable<T> iterable)
Used to avoid http://bugs.sun.com/view_bug.do?bug_id=6558557(package private) static boolean
containsAllImpl(java.util.Collection<?> self, java.util.Collection<?> c)
Returnstrue
if the collectionself
contains all of the elements in the collectionc
.static <E> java.util.Collection<E>
filter(java.util.Collection<E> unfiltered, Predicate<? super E> predicate)
Returns the elements ofunfiltered
that satisfy a predicate.private static boolean
isPermutation(java.util.List<?> first, java.util.List<?> second)
Returnstrue
if the second list is a permutation of the first.private static boolean
isPositiveInt(long n)
(package private) static java.lang.StringBuilder
newStringBuilderForCollection(int size)
Returns best-effort-sized StringBuilder based on the given collection size.static <E extends java.lang.Comparable<? super E>>
java.util.Collection<java.util.List<E>>orderedPermutations(java.lang.Iterable<E> elements)
Returns aCollection
of all the permutations of the specifiedIterable
.static <E> java.util.Collection<java.util.List<E>>
orderedPermutations(java.lang.Iterable<E> elements, java.util.Comparator<? super E> comparator)
Returns aCollection
of all the permutations of the specifiedIterable
using the specifiedComparator
for establishing the lexicographical ordering.static <E> java.util.Collection<java.util.List<E>>
permutations(java.util.Collection<E> elements)
Returns aCollection
of all the permutations of the specifiedCollection
.(package private) static boolean
safeContains(java.util.Collection<?> collection, java.lang.Object object)
Delegates toCollection.contains(java.lang.Object)
.(package private) static boolean
safeRemove(java.util.Collection<?> collection, java.lang.Object object)
Delegates toCollection.remove(java.lang.Object)
.(package private) static java.lang.String
toStringImpl(java.util.Collection<?> collection)
An implementation ofCollection#toString()
.static <F,T>
java.util.Collection<T>transform(java.util.Collection<F> fromCollection, Function<? super F,T> function)
Returns a collection that appliesfunction
to each element offromCollection
.
-
-
-
Field Detail
-
STANDARD_JOINER
static final Joiner STANDARD_JOINER
-
-
Method Detail
-
filter
public static <E> java.util.Collection<E> filter(java.util.Collection<E> unfiltered, Predicate<? super E> predicate)
Returns the elements ofunfiltered
that satisfy a predicate. The returned collection is a live view ofunfiltered
; changes to one affect the other.The resulting collection's iterator does not support
remove()
, but all other collection methods are supported. When given an element that doesn't satisfy the predicate, the collection'sadd()
andaddAll()
methods throw anIllegalArgumentException
. When methods such asremoveAll()
andclear()
are called on the filtered collection, only elements that satisfy the filter will be removed from the underlying collection.The returned collection isn't threadsafe or serializable, even if
unfiltered
is.Many of the filtered collection's methods, such as
size()
, iterate across every element in the underlying collection and determine which elements satisfy the filter. When a live view is not needed, it may be faster to copyIterables.filter(unfiltered, predicate)
and use the copy.Warning:
predicate
must be consistent with equals, as documented atPredicate.apply(T)
. Do not provide a predicate such asPredicates.instanceOf(ArrayList.class)
, which is inconsistent with equals. (SeeIterables.filter(Iterable, Class)
for related functionality.)
-
safeContains
static boolean safeContains(java.util.Collection<?> collection, @Nullable java.lang.Object object)
Delegates toCollection.contains(java.lang.Object)
. Returnsfalse
if thecontains
method throws aClassCastException
orNullPointerException
.
-
safeRemove
static boolean safeRemove(java.util.Collection<?> collection, @Nullable java.lang.Object object)
Delegates toCollection.remove(java.lang.Object)
. Returnsfalse
if theremove
method throws aClassCastException
orNullPointerException
.
-
transform
public static <F,T> java.util.Collection<T> transform(java.util.Collection<F> fromCollection, Function<? super F,T> function)
Returns a collection that appliesfunction
to each element offromCollection
. The returned collection is a live view offromCollection
; changes to one affect the other.The returned collection's
add()
andaddAll()
methods throw anUnsupportedOperationException
. All other collection methods are supported, as long asfromCollection
supports them.The returned collection isn't threadsafe or serializable, even if
fromCollection
is.When a live view is not needed, it may be faster to copy the transformed collection and use the copy.
If the input
Collection
is known to be aList
, considerLists.transform(java.util.List<F>, com.google.common.base.Function<? super F, ? extends T>)
. If only anIterable
is available, useIterables.transform(java.lang.Iterable<F>, com.google.common.base.Function<? super F, ? extends T>)
.
-
containsAllImpl
static boolean containsAllImpl(java.util.Collection<?> self, java.util.Collection<?> c)
Returnstrue
if the collectionself
contains all of the elements in the collectionc
.This method iterates over the specified collection
c
, checking each element returned by the iterator in turn to see if it is contained in the specified collectionself
. If all elements are so contained,true
is returned, otherwisefalse
.- Parameters:
self
- a collection which might contain all elements inc
c
- a collection whose elements might be contained byself
-
toStringImpl
static java.lang.String toStringImpl(java.util.Collection<?> collection)
An implementation ofCollection#toString()
.
-
newStringBuilderForCollection
static java.lang.StringBuilder newStringBuilderForCollection(int size)
Returns best-effort-sized StringBuilder based on the given collection size.
-
cast
static <T> java.util.Collection<T> cast(java.lang.Iterable<T> iterable)
Used to avoid http://bugs.sun.com/view_bug.do?bug_id=6558557
-
orderedPermutations
@Beta public static <E extends java.lang.Comparable<? super E>> java.util.Collection<java.util.List<E>> orderedPermutations(java.lang.Iterable<E> elements)
Returns aCollection
of all the permutations of the specifiedIterable
.Notes: This is an implementation of the algorithm for Lexicographical Permutations Generation, described in Knuth's "The Art of Computer Programming", Volume 4, Chapter 7, Section 7.2.1.2. The iteration order follows the lexicographical order. This means that the first permutation will be in ascending order, and the last will be in descending order.
Duplicate elements are considered equal. For example, the list [1, 1] will have only one permutation, instead of two. This is why the elements have to implement
Comparable
.An empty iterable has only one permutation, which is an empty list.
This method is equivalent to
Collections2.orderedPermutations(list, Ordering.natural())
.- Parameters:
elements
- the original iterable whose elements have to be permuted.- Returns:
- an immutable
Collection
containing all the different permutations of the original iterable. - Throws:
java.lang.NullPointerException
- if the specified iterable is null or has any null elements.- Since:
- 12.0
-
orderedPermutations
@Beta public static <E> java.util.Collection<java.util.List<E>> orderedPermutations(java.lang.Iterable<E> elements, java.util.Comparator<? super E> comparator)
Returns aCollection
of all the permutations of the specifiedIterable
using the specifiedComparator
for establishing the lexicographical ordering.Examples:
for (List<String> perm : orderedPermutations(asList("b", "c", "a"))) { println(perm); } // -> ["a", "b", "c"] // -> ["a", "c", "b"] // -> ["b", "a", "c"] // -> ["b", "c", "a"] // -> ["c", "a", "b"] // -> ["c", "b", "a"] for (List<Integer> perm : orderedPermutations(asList(1, 2, 2, 1))) { println(perm); } // -> [1, 1, 2, 2] // -> [1, 2, 1, 2] // -> [1, 2, 2, 1] // -> [2, 1, 1, 2] // -> [2, 1, 2, 1] // -> [2, 2, 1, 1]
Notes: This is an implementation of the algorithm for Lexicographical Permutations Generation, described in Knuth's "The Art of Computer Programming", Volume 4, Chapter 7, Section 7.2.1.2. The iteration order follows the lexicographical order. This means that the first permutation will be in ascending order, and the last will be in descending order.
Elements that compare equal are considered equal and no new permutations are created by swapping them.
An empty iterable has only one permutation, which is an empty list.
- Parameters:
elements
- the original iterable whose elements have to be permuted.comparator
- a comparator for the iterable's elements.- Returns:
- an immutable
Collection
containing all the different permutations of the original iterable. - Throws:
java.lang.NullPointerException
- If the specified iterable is null, has any null elements, or if the specified comparator is null.- Since:
- 12.0
-
permutations
@Beta public static <E> java.util.Collection<java.util.List<E>> permutations(java.util.Collection<E> elements)
Returns aCollection
of all the permutations of the specifiedCollection
.Notes: This is an implementation of the Plain Changes algorithm for permutations generation, described in Knuth's "The Art of Computer Programming", Volume 4, Chapter 7, Section 7.2.1.2.
If the input list contains equal elements, some of the generated permutations will be equal.
An empty collection has only one permutation, which is an empty list.
- Parameters:
elements
- the original collection whose elements have to be permuted.- Returns:
- an immutable
Collection
containing all the different permutations of the original collection. - Throws:
java.lang.NullPointerException
- if the specified collection is null or has any null elements.- Since:
- 12.0
-
isPermutation
private static boolean isPermutation(java.util.List<?> first, java.util.List<?> second)
Returnstrue
if the second list is a permutation of the first.
-
isPositiveInt
private static boolean isPositiveInt(long n)
-
-