javadoc.com.google.common.collect.Collections2.html Maven / Gradle / Ivy
Collections2 (Guava: Google Core Libraries for Java 11.0.1 API)
Overview
Package
Class
Use
Tree
Deprecated
Index
Help
PREV CLASS
NEXT CLASS
FRAMES
NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD
DETAIL: FIELD | CONSTR | METHOD
com.google.common.collect
Class Collections2
java.lang.Object
com.google.common.collect.Collections2
@GwtCompatible
public final class Collections2
- extends Object
Provides static methods for working with Collection
instances.
- Since:
- 2.0 (imported from Google Collections Library)
- Author:
- Chris Povirk, Mike Bostock, Jared Levy
Method Summary | ||
---|---|---|
static
|
filter(Collection<E> unfiltered,
Predicate<? super E> predicate)
Returns the elements of unfiltered that satisfy a predicate. |
|
static
|
transform(Collection<F> fromCollection,
Function<? super F,T> function)
Returns a collection that applies function to each element of
fromCollection . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
filter
public static <E> Collection<E> filter(Collection<E> unfiltered, Predicate<? super E> predicate)
- Returns the elements of
unfiltered
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.)
transform
public static <F,T> Collection<T> transform(Collection<F> fromCollection, Function<? super F,T> function)
- Returns a collection that applies
function
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
. If only an, com.google.common.base.Function super F, ? extends T>) Iterable
is available, useIterables.transform(java.lang.Iterable
., com.google.common.base.Function super F, ? extends T>)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright © 2010-2012. All Rights Reserved.