public class CollectionUtils
extends java.lang.Object
| Modifier and Type | Method and Description |
|---|---|
static <T> void |
addAll(java.util.Collection<T> collection,
java.lang.Iterable<? extends T> items)
Add all the items from an iterable to a collection.
|
static double[] |
asDoubleArray(java.util.Collection<java.lang.Double> coll) |
static int[] |
asIntArray(java.util.Collection<java.lang.Integer> coll) |
static java.util.List<java.lang.Double> |
asList(double[] a) |
static java.util.List<java.lang.Integer> |
asList(int[] a) |
static <T> java.util.Set<T> |
asSet(T... o)
Returns a new Set containing all the objects in the specified array.
|
static <T extends java.lang.Comparable<T>> |
compareLists(java.util.List<T> list1,
java.util.List<T> list2)
Provides a consistent ordering over lists.
|
static <E> java.util.Iterator<E> |
concatIterators(java.util.Iterator<E>... iterators)
Concatenate a number of iterators together, to form one big iterator.
|
static <T> boolean |
containsAny(java.util.Collection<T> collection,
java.util.Collection<T> toCheck)
if any item in toCheck is present in collection
|
static <T> boolean |
containsObject(java.util.Collection<T> c,
T o)
Checks whether a Collection contains a specified Object.
|
static <T> java.util.Collection<T> |
diff(java.util.Collection<T> list1,
java.util.Collection<T> list2)
Returns all objects in list1 that are not in list2.
|
static <T> java.util.Set<T> |
diffAsSet(java.util.Collection<T> list1,
java.util.Collection<T> list2)
Returns all objects in list1 that are not in list2.
|
static <T> java.util.List<T> |
filterAsList(java.util.Collection<? extends T> original,
java.util.function.Predicate<? super T> f)
Filters the objects in the collection according to the given Filter and returns a list.
|
static <T> java.util.List<T> |
flatten(java.util.Collection<java.util.List<T>> nestedList)
Combines all the lists in a collection to a single list.
|
static <T,V> java.util.List<V> |
getAll(java.util.Map<T,V> map,
java.util.Collection<T> indices)
Get all values corresponding to the indices (if they exist in the map).
|
static <T> int |
getIndex(java.util.List<T> l,
T o)
Returns the index of the first occurrence in the list of the specified
object, using object identity (==) not equality as the criterion for object
presence.
|
static <T> int |
getIndex(java.util.List<T> l,
T o,
int fromIndex)
Returns the index of the first occurrence after the startIndex (exclusive)
in the list of the specified object, using object equals function.
|
static <C extends java.lang.Comparable<C>> |
getListComparator() |
static <K,V> java.util.Map<K,V> |
getMapFromString(java.lang.String s,
java.lang.Class<K> keyClass,
java.lang.Class<V> valueClass,
MapFactory<K,V> mapFactory) |
static <T> java.util.List<java.util.List<T>> |
getNGrams(java.util.List<T> items,
int minSize,
int maxSize)
Get all sub-lists of the given list of the given sizes.
|
static <T> java.util.List<java.util.List<T>> |
getPrefixesAndSuffixes(java.util.List<T> items,
int minSize,
int maxSize,
T paddingSymbol,
boolean includePrefixes,
boolean includeSuffixes)
Get all prefix/suffix combinations from a list.
|
static <T> java.util.Set<T> |
intersection(java.util.Set<T> set1,
java.util.Set<T> set2) |
static <T> boolean |
isSubList(java.util.List<T> l1,
java.util.List<? super T> l)
Returns true iff l1 is a sublist of l (i.e., every member of l1 is in l,
and for every e1 < e2 in l1, there is an e1 < e2 occurrence in l).
|
static <T> void |
loadCollection(java.io.File file,
java.lang.Class<T> itemClass,
java.util.Collection<T> collection)
Adds the items from the file to the collection.
|
static <T> java.util.Collection<T> |
loadCollection(java.io.File file,
java.lang.Class<T> c,
CollectionFactory<T> cf) |
static <T> void |
loadCollection(java.lang.String fileName,
java.lang.Class<T> itemClass,
java.util.Collection<T> collection)
Adds the items from the file to the collection.
|
static <T> java.util.Collection<T> |
loadCollection(java.lang.String filename,
java.lang.Class<T> c,
CollectionFactory<T> cf) |
static <T> java.util.List<T> |
makeList(T... items)
Returns a new List containing the given objects.
|
static <T extends java.lang.Comparable<? super T>> |
maxIndex(java.util.List<T> list) |
static <T,M> java.util.List<T> |
mergeList(java.util.List<? extends T> list,
java.util.Collection<M> matched,
java.util.function.Function<M,Interval<java.lang.Integer>> toIntervalFunc,
java.util.function.Function<java.util.List<? extends T>,T> aggregator) |
static <T> java.util.List<T> |
mergeList(java.util.List<? extends T> list,
java.util.List<? extends HasInterval<java.lang.Integer>> matched,
java.util.function.Function<java.util.List<? extends T>,T> aggregator) |
static <T> java.util.List<T> |
mergeListWithSortedMatched(java.util.List<? extends T> list,
java.util.List<? extends HasInterval<java.lang.Integer>> matched,
java.util.function.Function<java.util.List<? extends T>,T> aggregator) |
static <T> java.util.List<T> |
mergeListWithSortedMatchedPreAggregated(java.util.List<? extends T> list,
java.util.List<? extends T> matched,
java.util.function.Function<T,Interval<java.lang.Integer>> toIntervalFunc) |
static <T> T |
mode(java.util.Collection<T> values)
Returns the mode in the Collection.
|
static <T> java.util.Set<T> |
modes(java.util.Collection<T> values)
Returns a list of all modes in the Collection.
|
static <T> java.util.List<java.util.Collection<T>> |
partitionIntoFolds(java.util.List<T> values,
int numFolds)
Split a list into numFolds (roughly) equally sized folds.
|
static <T> boolean |
removeObject(java.util.List<T> l,
T o)
Removes the first occurrence in the list of the specified object, using
object identity (==) not equality as the criterion for object presence.
|
static <E> E |
sample(java.util.List<E> l,
java.util.Random r) |
static <E> java.util.Collection<E> |
sampleWithoutReplacement(java.util.Collection<E> c,
int n)
Samples without replacement from a collection.
|
static <E> java.util.Collection<E> |
sampleWithoutReplacement(java.util.Collection<E> c,
int n,
java.util.Random r)
Samples without replacement from a collection, using your own
Random number generator. |
static <E> java.util.Collection<E> |
sampleWithReplacement(java.util.Collection<E> c,
int n)
Samples with replacement from a collection.
|
static <E> java.util.Collection<E> |
sampleWithReplacement(java.util.Collection<E> c,
int n,
java.util.Random r)
Samples with replacement from a collection, using your own
Random
number generator. |
static <T extends java.lang.Comparable<T>> |
sorted(java.lang.Iterable<T> items)
Return the items of an Iterable as a sorted list.
|
static <T> java.util.List<T> |
sorted(java.lang.Iterable<T> items,
java.util.Comparator<T> comparator)
Return the items of an Iterable as a sorted list.
|
static <T> java.util.List<T> |
toList(java.lang.Iterable<T> items)
Create a list out of the items in the Iterable.
|
static <T> java.util.Set<T> |
toSet(java.lang.Iterable<T> items)
Create a set out of the items in the Iterable.
|
static <K,V> java.lang.String |
toVerticalString(java.util.Map<K,V> m) |
static <T> java.util.Collection<Pair<java.util.Collection<T>,java.util.Collection<T>>> |
trainTestFoldsForCV(java.util.List<T> values,
int numFolds)
Split a list into train, test pairs for use in k-fold crossvalidation.
|
static <T1,T2> java.util.List<T2> |
transformAsList(java.util.Collection<? extends T1> original,
java.util.function.Function<T1,? extends T2> f)
Transforms the keyset of collection according to the given Function and returns a list.
|
static <T1,T2> java.util.Set<T2> |
transformAsSet(java.util.Collection<? extends T1> original,
java.util.function.Function<T1,? extends T2> f)
Transforms the keyset of collection according to the given Function and returns a set of the keys.
|
static <T> java.util.Collection<T> |
union(java.util.Collection<T> set1,
java.util.Collection<T> set2) |
static <T> java.util.Set<T> |
unionAsSet(java.util.Collection<T>... sets) |
static <T> java.util.Set<T> |
unionAsSet(java.util.Collection<T> set1,
java.util.Collection<T> set2) |
static <ObjType,Hashable> |
uniqueNonhashableObjects(java.util.Collection<ObjType> objects,
java.util.function.Function<ObjType,Hashable> customHasher)
Makes it possible to uniquify a collection of objects which are normally
non-hashable.
|
public static java.util.List<java.lang.Integer> asList(int[] a)
public static java.util.List<java.lang.Double> asList(double[] a)
public static int[] asIntArray(java.util.Collection<java.lang.Integer> coll)
public static double[] asDoubleArray(java.util.Collection<java.lang.Double> coll)
@SafeVarargs public static <T> java.util.List<T> makeList(T... items)
@SafeVarargs public static <T> java.util.Set<T> asSet(T... o)
public static <T> java.util.Set<T> intersection(java.util.Set<T> set1,
java.util.Set<T> set2)
public static <T> java.util.Collection<T> union(java.util.Collection<T> set1,
java.util.Collection<T> set2)
public static <T> java.util.Set<T> unionAsSet(java.util.Collection<T> set1,
java.util.Collection<T> set2)
@SafeVarargs public static <T> java.util.Set<T> unionAsSet(java.util.Collection<T>... sets)
public static <T> java.util.Collection<T> diff(java.util.Collection<T> list1,
java.util.Collection<T> list2)
T - Type of items in the collectionlist1 - First collectionlist2 - Second collectionpublic static <T> java.util.Set<T> diffAsSet(java.util.Collection<T> list1,
java.util.Collection<T> list2)
T - Type of items in the collectionlist1 - First collectionlist2 - Second collectionpublic static <T> java.util.Collection<T> loadCollection(java.lang.String filename,
java.lang.Class<T> c,
CollectionFactory<T> cf)
throws java.lang.Exception
filename - The path to the file to load the List fromc - The Class to instantiate each member of the List. Must have a
String constructor.java.lang.Exceptionpublic static <T> java.util.Collection<T> loadCollection(java.io.File file,
java.lang.Class<T> c,
CollectionFactory<T> cf)
throws java.lang.Exception
file - The file to load the List fromc - The Class to instantiate each member of the List. Must have a
String constructor.java.lang.Exceptionpublic static <T> void loadCollection(java.lang.String fileName,
java.lang.Class<T> itemClass,
java.util.Collection<T> collection)
throws java.lang.NoSuchMethodException,
java.lang.InstantiationException,
java.lang.IllegalAccessException,
java.lang.reflect.InvocationTargetException,
java.io.IOException
T - The type of the items.fileName - The name of the file from which items should be loaded.itemClass - The class of the items (must have a constructor that accepts a String).collection - The collection to which items should be added.java.lang.NoSuchMethodExceptionjava.lang.InstantiationExceptionjava.lang.IllegalAccessExceptionjava.lang.reflect.InvocationTargetExceptionjava.io.IOExceptionpublic static <T> void loadCollection(java.io.File file,
java.lang.Class<T> itemClass,
java.util.Collection<T> collection)
throws java.lang.NoSuchMethodException,
java.lang.InstantiationException,
java.lang.IllegalAccessException,
java.lang.reflect.InvocationTargetException,
java.io.IOException
T - The type of the items.file - The file from which items should be loaded.itemClass - The class of the items (must have a constructor that accepts a String).collection - The collection to which items should be added.java.lang.NoSuchMethodExceptionjava.lang.InstantiationExceptionjava.lang.IllegalAccessExceptionjava.lang.reflect.InvocationTargetExceptionjava.io.IOExceptionpublic static <K,V> java.util.Map<K,V> getMapFromString(java.lang.String s,
java.lang.Class<K> keyClass,
java.lang.Class<V> valueClass,
MapFactory<K,V> mapFactory)
throws java.lang.ClassNotFoundException,
java.lang.NoSuchMethodException,
java.lang.IllegalAccessException,
java.lang.reflect.InvocationTargetException,
java.lang.InstantiationException
java.lang.ClassNotFoundExceptionjava.lang.NoSuchMethodExceptionjava.lang.IllegalAccessExceptionjava.lang.reflect.InvocationTargetExceptionjava.lang.InstantiationExceptionpublic static <T> boolean containsObject(java.util.Collection<T> c,
T o)
public static <T> boolean removeObject(java.util.List<T> l,
T o)
l - The List from which to remove the objecto - The object to be removed.public static <T> int getIndex(java.util.List<T> l,
T o)
l - The List to find the object in.o - The sought-after object.public static <T> int getIndex(java.util.List<T> l,
T o,
int fromIndex)
l - The List to find the object in.o - The sought-after object.fromIndex - The start indexpublic static <E> java.util.Collection<E> sampleWithoutReplacement(java.util.Collection<E> c,
int n)
c - The collection to be sampled fromn - The number of samples to takepublic static <E> java.util.Collection<E> sampleWithoutReplacement(java.util.Collection<E> c,
int n,
java.util.Random r)
Random number generator.c - The collection to be sampled fromn - The number of samples to taker - The random number generatorpublic static <E> E sample(java.util.List<E> l,
java.util.Random r)
public static <E> java.util.Collection<E> sampleWithReplacement(java.util.Collection<E> c,
int n)
c - The collection to be sampled fromn - The number of samples to takepublic static <E> java.util.Collection<E> sampleWithReplacement(java.util.Collection<E> c,
int n,
java.util.Random r)
Random
number generator.c - The collection to be sampled fromn - The number of samples to taker - The random number generatorpublic static <T> boolean isSubList(java.util.List<T> l1,
java.util.List<? super T> l)
public static <K,V> java.lang.String toVerticalString(java.util.Map<K,V> m)
public static <T extends java.lang.Comparable<T>> int compareLists(java.util.List<T> list1,
java.util.List<T> list2)
public static <C extends java.lang.Comparable<C>> java.util.Comparator<java.util.List<C>> getListComparator()
public static <T extends java.lang.Comparable<T>> java.util.List<T> sorted(java.lang.Iterable<T> items)
T - The type of items in the Iterable.items - The collection to be sorted.public static <T> java.util.List<T> sorted(java.lang.Iterable<T> items,
java.util.Comparator<T> comparator)
T - The type of items in the Iterable.items - The collection to be sorted.public static <T> java.util.List<T> toList(java.lang.Iterable<T> items)
T - The type of items in the Iterable.items - The items to be made into a list.public static <T> java.util.Set<T> toSet(java.lang.Iterable<T> items)
T - The type of items in the Iterable.items - The items to be made into a set.public static <T> void addAll(java.util.Collection<T> collection,
java.lang.Iterable<? extends T> items)
T - The type of items in the iterable and the collectioncollection - The collection to which the items should be added.items - The items to add to the collection.public static <T> java.util.List<java.util.List<T>> getNGrams(java.util.List<T> items,
int minSize,
int maxSize)
List<String> items = Arrays.asList("a", "b", "c", "d");
System.out.println(CollectionUtils.getNGrams(items, 1, 2));
would print out:
[[a], [a, b], [b], [b, c], [c], [c, d], [d]]
T - The type of items contained in the list.items - The list of items.minSize - The minimum size of an ngram.maxSize - The maximum size of an ngram.public static <T> java.util.List<java.util.List<T>> getPrefixesAndSuffixes(java.util.List<T> items,
int minSize,
int maxSize,
T paddingSymbol,
boolean includePrefixes,
boolean includeSuffixes)
List<String> items = Arrays.asList("a", "b", "c", "d");
System.out.println(CollectionUtils.getPrefixesAndSuffixes(items, 1, 2, null, true, true));
would print out:
[[d], [a], [a, d], [d, c], [a, b], [a, b, c, d]]and
List<String> items2 = Arrays.asList("a");
System.out.println(CollectionUtils.getPrefixesAndSuffixes(items2, 1, 2, null, true, true));
would print:
[[a], [a], [a, a], [a, null], [a, null], [a, null, a, null]]
T - The type of items contained in the list.items - The list of items.minSize - The minimum length of a prefix/suffix span (should be at least 1)maxSize - The maximum length of a prefix/suffix spanpaddingSymbol - Symbol to be included if we run out of bounds (e.g. if items has
size 3 and we try to extract a span of length 4).includePrefixes - Whether to extract prefixesincludeSuffixes - Whether to extract suffixespublic static <T,M> java.util.List<T> mergeList(java.util.List<? extends T> list,
java.util.Collection<M> matched,
java.util.function.Function<M,Interval<java.lang.Integer>> toIntervalFunc,
java.util.function.Function<java.util.List<? extends T>,T> aggregator)
public static <T> java.util.List<T> mergeList(java.util.List<? extends T> list,
java.util.List<? extends HasInterval<java.lang.Integer>> matched,
java.util.function.Function<java.util.List<? extends T>,T> aggregator)
public static <T> java.util.List<T> mergeListWithSortedMatched(java.util.List<? extends T> list,
java.util.List<? extends HasInterval<java.lang.Integer>> matched,
java.util.function.Function<java.util.List<? extends T>,T> aggregator)
public static <T> java.util.List<T> mergeListWithSortedMatchedPreAggregated(java.util.List<? extends T> list,
java.util.List<? extends T> matched,
java.util.function.Function<T,Interval<java.lang.Integer>> toIntervalFunc)
public static <T> java.util.List<T> flatten(java.util.Collection<java.util.List<T>> nestedList)
public static <ObjType,Hashable> java.util.Collection<ObjType> uniqueNonhashableObjects(java.util.Collection<ObjType> objects,
java.util.function.Function<ObjType,Hashable> customHasher)
public static <T> boolean containsAny(java.util.Collection<T> collection,
java.util.Collection<T> toCheck)
collection - toCheck - public static <T> java.util.List<java.util.Collection<T>> partitionIntoFolds(java.util.List<T> values,
int numFolds)
public static <T> java.util.Collection<Pair<java.util.Collection<T>,java.util.Collection<T>>> trainTestFoldsForCV(java.util.List<T> values, int numFolds)
public static <T> java.util.Set<T> modes(java.util.Collection<T> values)
public static <T> T mode(java.util.Collection<T> values)
public static <T1,T2> java.util.Set<T2> transformAsSet(java.util.Collection<? extends T1> original,
java.util.function.Function<T1,? extends T2> f)
public static <T1,T2> java.util.List<T2> transformAsList(java.util.Collection<? extends T1> original,
java.util.function.Function<T1,? extends T2> f)
public static <T> java.util.List<T> filterAsList(java.util.Collection<? extends T> original,
java.util.function.Predicate<? super T> f)
public static <T,V> java.util.List<V> getAll(java.util.Map<T,V> map,
java.util.Collection<T> indices)
map - Any map from T to Vindices - A collection of indices of type Tpublic static <T extends java.lang.Comparable<? super T>> int maxIndex(java.util.List<T> list)
@SafeVarargs public static <E> java.util.Iterator<E> concatIterators(java.util.Iterator<E>... iterators)
E - The type of the iterators.iterators - The iterators to concatenate.