public class CollectionUtil extends Object
Constructor and Description |
---|
CollectionUtil() |
Modifier and Type | Method and Description |
---|---|
static <T,E extends T,C extends Collection<T>> |
addAll(C col,
E[] values)
Adds all items in the given object array to the supplied collection and
returns the supplied collection.
|
static <T,C extends Collection<T>> |
addAll(C col,
Enumeration<? extends T> enm)
Adds all items returned by the enumeration to the supplied collection
and returns the supplied collection.
|
static <T,C extends Collection<T>> |
addAll(C col,
Iterable<? extends Collection<? extends T>> values)
Folds all the specified values into the supplied collection and returns it.
|
static <T,C extends Collection<T>> |
addAll(C col,
Iterator<? extends T> iter)
Adds all items returned by the iterator to the supplied collection and
returns the supplied collection.
|
static void |
limit(Collection<?> col,
int limit)
Modify the specified Collection so that only the first
limit elements
remain, as determined by iteration order. |
static <T extends Comparable<? super T>> |
maxList(Iterable<T> iterable)
Return a List containing all the elements of the specified Iterable that compare as being
equal to the maximum element.
|
static <T> List<T> |
maxList(Iterable<T> iterable,
Comparator<? super T> comp)
Return a List containing all the elements of the specified Iterable that compare as being
equal to the maximum element.
|
static <T extends Comparable<? super T>> |
minList(Iterable<T> iterable)
Return a List containing all the elements of the specified Iterable that compare as being
equal to the minimum element.
|
static <T> List<T> |
minList(Iterable<T> iterable,
Comparator<? super T> comp)
Return a List containing all the elements of the specified Iterable that compare as being
equal to the minimum element.
|
static <K,V,M extends Map<K,V>> |
putAll(M map,
Iterable<? extends Map<? extends K,? extends V>> values)
Folds all the specified values into the supplied map and returns it.
|
static <T> List<T> |
selectRandomSubset(Collection<T> col,
int count)
Returns a list containing a random selection of elements from the
specified collection.
|
static <T> T[] |
toArray(Collection<? extends T> col,
Class<T> type)
Returns an Array, of the type specified by the runtime-type token
type ,
containing the elements of the collection. |
static int[] |
toIntArray(Collection<Integer> col)
If a collection contains only
Integer objects, it can be
passed to this function and converted into an int array. |
@ReplacedBy(value="com.google.common.collect.Iterators#addAll(Collection, com.google.common.collect.Iterators#forEnumeration(Enumeration))") public static <T,C extends Collection<T>> C addAll(C col, Enumeration<? extends T> enm)
@ReplacedBy(value="com.google.common.collect.Iterators#addAll()") public static <T,C extends Collection<T>> C addAll(C col, Iterator<? extends T> iter)
@ReplacedBy(value="java.util.Collections#addAll()") public static <T,E extends T,C extends Collection<T>> C addAll(C col, E[] values)
public static <T,C extends Collection<T>> C addAll(C col, Iterable<? extends Collection<? extends T>> values)
public static <K,V,M extends Map<K,V>> M putAll(M map, Iterable<? extends Map<? extends K,? extends V>> values)
public static void limit(Collection<?> col, int limit)
limit
elements
remain, as determined by iteration order. If the Collection is smaller than limit,
it is unmodified.public static <T extends Comparable<? super T>> List<T> maxList(Iterable<T> iterable)
NoSuchElementException
- if the Iterable is empty.public static <T> List<T> maxList(Iterable<T> iterable, Comparator<? super T> comp)
NoSuchElementException
- if the Iterable is empty.public static <T extends Comparable<? super T>> List<T> minList(Iterable<T> iterable)
NoSuchElementException
- if the Iterable is empty.public static <T> List<T> minList(Iterable<T> iterable, Comparator<? super T> comp)
NoSuchElementException
- if the Iterable is empty.public static <T> List<T> selectRandomSubset(Collection<T> col, int count)
count
, each element in the source collection will
be selected with equal probability and no element will be included more
than once. The elements in the random subset will always be included in
the order they are returned from the source collection's iterator.
Algorithm courtesy of William R. Mahoney, published in Dr. Dobbs Journal, February 2002.
IllegalArgumentException
- thrown if the size of the collection
is smaller than the number of elements requested for the random subset.@ReplacedBy(value="com.google.common.collect.Iterables#toArray()") public static <T> T[] toArray(Collection<? extends T> col, Class<T> type)
type
,
containing the elements of the collection.@ReplacedBy(value="com.google.common.primitives.Ints#toArray()") public static int[] toIntArray(Collection<Integer> col)
Integer
objects, it can be
passed to this function and converted into an int array.col
- the collection to be converted.Copyright © 2015. All rights reserved.