@CodeReview(reviewer="Kevin R. Dixon", date="2008-12-02", changesNeeded=false, comments="Looks good.") public class CollectionUtil extends java.lang.Object
CollectionUtil
class implements static methods for dealing with
Collection
and Iterable
objects. They are both put into the
same utility class so that they can be interchanged without changing the
method call.Constructor and Description |
---|
CollectionUtil() |
Modifier and Type | Method and Description |
---|---|
static <DataType> java.util.ArrayList<DataType> |
asArrayList(java.lang.Iterable<DataType> data)
Returns the Collection as an ArrayList.
|
static <DataType> java.util.ArrayList<DataType> |
createArrayList(DataType first,
DataType second)
Creates a new ArrayList from the given pair of values.
|
static <KeyType,ValueType> |
createHashMapWithSize(int size)
Creates a new
HashMap with the given expected size. |
static <ValueType> |
createHashSetWithSize(int size)
Creates a new
HashSet with the given expected size. |
static <KeyType,ValueType> |
createLinkedHashMapWithSize(int size)
Creates a new
LinkedHashMap with the given expected size. |
static <ValueType> |
createLinkedHashSetWithSize(int size)
Creates a new
LinkedHashSet with the given expected size. |
static <DataType> java.util.ArrayList<java.util.List<? extends DataType>> |
createSequentialPartitions(java.lang.Iterable<? extends DataType> data,
int numPartitions)
Creates a partition of the given data into "numPartition" roughly equal
sets, preserving their pre-existing sequential ordering, with the nonzero
remainder elements going into the final partition.
|
static <DataType> java.util.ArrayList<java.util.List<? extends DataType>> |
createSequentialPartitions(java.util.List<? extends DataType> data,
int numPartitions)
Creates a partition of the given data into "numPartition" roughly equal
sets, preserving their pre-existing sequential ordering, with the nonzero
remainder elements going into the final partition.
|
static <T> boolean |
equals(java.util.Collection<? extends T> data1,
java.util.Collection<? extends T> data2)
Check if two collections return exactly the same objects in the same
order.
|
static <T> boolean |
equals(java.lang.Iterable<? extends T> data1,
java.lang.Iterable<? extends T> data2)
Check if two iterables return exactly the same objects in the same order.
|
static <ComparableType> |
findKthLargest(int k,
java.util.ArrayList<? extends ComparableType> data,
java.util.Comparator<? super ComparableType> comparator)
Returns the set of indices of the data array such that
data[return[0..k-1]] ≤ data[return[k]] ≤ data[return[k+1...N-1]].
|
static <DataType> DataType |
getElement(java.lang.Iterable<DataType> iterable,
int index)
Returns the indexed value into the
Iterable . |
static <T> T |
getFirst(java.lang.Iterable<? extends T> iterable)
Gets the first element from an iterable.
|
static <T> T |
getFirst(java.util.List<? extends T> list)
Gets the first element of the list.
|
static <T> T |
getLast(java.util.List<? extends T> list)
Gets the last element of the list.
|
static boolean |
isEmpty(java.util.Collection<?> collection)
Returns true if the given collection is null or empty.
|
static boolean |
isEmpty(java.lang.Iterable<?> iterable)
Returns true if the given iterable is null or empty.
|
static <DataType> DataType |
removeElement(java.lang.Iterable<DataType> iterable,
int index)
Removes and returns the indexed value into the
Iterable . |
static int |
size(java.util.Collection<?> collection)
Determines the size of the given collection, checking for null.
|
static int |
size(java.lang.Iterable<?> iterable)
Determines the size of the given iterable.
|
static java.lang.String |
toStringDelimited(java.lang.Iterable<?> list,
java.lang.String delimiter)
Performs a toString on each element given iterable with a given delimiter
between elements.
|
public static boolean isEmpty(java.util.Collection<?> collection)
collection
- The collection to determine if it is null or empty.public static boolean isEmpty(java.lang.Iterable<?> iterable)
iterable
- The iterable to determine if it is null or empty.public static <DataType> java.util.ArrayList<DataType> asArrayList(java.lang.Iterable<DataType> data)
DataType
- Type of data in the Collection.data
- Collection to return as an ArrayList.public static int size(java.util.Collection<?> collection)
collection
- The collection to get the size of.public static int size(java.lang.Iterable<?> iterable)
Collection
, then the size method is used.
Otherwise, the iterable is iterated over to get the size.iterable
- The iterable to determine the size of.public static <T> T getFirst(java.lang.Iterable<? extends T> iterable)
T
- The type of element.iterable
- The iterable to get the first element from.public static <T> T getFirst(java.util.List<? extends T> list)
T
- The type of element in the list.list
- The list to get the first element from.public static <T> T getLast(java.util.List<? extends T> list)
T
- The type of element in the list.list
- The list to get the last element from.public static <T> boolean equals(java.util.Collection<? extends T> data1, java.util.Collection<? extends T> data2)
T
- data1
- data2
- true
if simultaneous iteration of data1
and
data2
always returns objects for which
Object.equals(java.lang.Object)
is true
; false
otherwisepublic static <T> boolean equals(java.lang.Iterable<? extends T> data1, java.lang.Iterable<? extends T> data2)
T
- data1
- data2
- true
if simultaneous iteration of data1
and
data2
always returns objects for which
Object.equals(java.lang.Object)
is true
; false
otherwise@PublicationReference(author={"William H Press","Saul A. Teukolsky","William T. Vetterling","Brian P. Flannery"}, title="Numerical Recipes, Third Edition", type=Book, year=2007, pages=1104, notes="Loosely based on the selecti() function") public static <ComparableType> int[] findKthLargest(int k, java.util.ArrayList<? extends ComparableType> data, java.util.Comparator<? super ComparableType> comparator)
ComparableType
- Type of data to compare to.k
- kth largest value to split upon.data
- Data to partition, left unchanged by this method.comparator
- Comparator used to determine if two values are greater
than, less than, or equal to each other.public static <DataType> java.util.ArrayList<java.util.List<? extends DataType>> createSequentialPartitions(java.lang.Iterable<? extends DataType> data, int numPartitions)
DataType
- Type of data to partition.data
- Collection of data to partitionnumPartitions
- Number of partitions to create.public static <DataType> java.util.ArrayList<java.util.List<? extends DataType>> createSequentialPartitions(java.util.List<? extends DataType> data, int numPartitions)
DataType
- Type of data to partition.data
- Collection of data to partitionnumPartitions
- Number of partitions to create.public static <DataType> DataType getElement(java.lang.Iterable<DataType> iterable, int index)
Iterable
. It first checks to
see if the Iterable
is a List
, and if so calls the get
method. Otherwise, it walks the Iterable
to get to the element.DataType
- The type of data.iterable
- The iterable to pull the value from.index
- The 0-based index to pull from the iterable.java.lang.IndexOutOfBoundsException
- If the index is less than zero or
greater than or equal to the number of elements in the iterable.public static <DataType> DataType removeElement(java.lang.Iterable<DataType> iterable, int index)
Iterable
. It first
checks to see if the Iterable
is a List
, and if so calls
the remove method. Otherwise, it walks the Iterable
to get to the
element and remove it. This only works on Iterable
s that are
List
s or whose Iterator
implements the optional
remove
method.DataType
- The type of data.iterable
- The iterable to remove the value from.index
- The 0-based index to remove from the iterable.java.lang.IndexOutOfBoundsException
- If the index is less than zero or
greater than or equal to the number of elements in the iterable.java.lang.UnsupportedOperationException
- If the iterable does not support
remove.public static java.lang.String toStringDelimited(java.lang.Iterable<?> list, java.lang.String delimiter)
list
- The list to call toString on each element for.delimiter
- The delimiter.public static <DataType> java.util.ArrayList<DataType> createArrayList(DataType first, DataType second)
DataType
- The data type.first
- The first value.second
- The second value.public static <KeyType,ValueType> java.util.HashMap<KeyType,ValueType> createHashMapWithSize(int size)
HashMap
with the given expected size. It uses the
default load factor (0.75) to estimate the proper number of elements for
the data structure to avoid a rehash or resize when the given number of
elements are added.KeyType
- The type for the key of the map.ValueType
- The type of the value in the map.size
- The size. Must be positive.public static <KeyType,ValueType> java.util.LinkedHashMap<KeyType,ValueType> createLinkedHashMapWithSize(int size)
LinkedHashMap
with the given expected size. It uses
the default load factor (0.75) to estimate the proper number of elements
for the data structure to avoid a rehash or resize when the given number
of elements are added.KeyType
- The type for the key of the map.ValueType
- The type of the value in the map.size
- The size. Must be positive.public static <ValueType> java.util.HashSet<ValueType> createHashSetWithSize(int size)
HashSet
with the given expected size. It uses the
default load factor (0.75) to estimate the proper number of elements for
the data structure to avoid a rehash or resize when the given number of
elements are added.ValueType
- The type of the value in the set.size
- The size. Must be positive.public static <ValueType> java.util.LinkedHashSet<ValueType> createLinkedHashSetWithSize(int size)
LinkedHashSet
with the given expected size. It uses
the default load factor (0.75) to estimate the proper number of elements
for the data structure to avoid a rehash or resize when the given number
of elements are added.ValueType
- The type of the value in the set.size
- The size. Must be positive.