public class CollectionUtils extends Object
Provides extra functionality for the Java Collections Framework. For Java primitives check
the corresponding xxxUtils
class.
Modifier and Type | Method and Description |
---|---|
static <A> boolean |
contains(Collection<A> collection,
A value)
Checks if an input collection contains a given value.
|
static <A> boolean |
containsAll(Collection<A> container,
Collection<A> collection)
Checks whether all elements of a collection are present in another one.
|
static <A> boolean |
containsAny(Collection<A> collection1,
Collection<A> collection2)
Checks whether any element of a collection is present in another one.
|
static <A extends Number> |
convertId2LinearIndexMap(Set<A> elements)
Returns a map containing the association of each element with its corresponding
position within the set (in linear order).
|
static <A> Map<A,Integer> |
count(Collection<A> collection)
Returns a map where each entry represents the number of times that the
corresponding key appears in the input collection.
|
static <A,B> List<A> |
find(Map<A,B> map,
B value,
Constants.SearchType searchType)
Returns the key(s) where a given value can be found into a map.
|
static <A,B> org.apache.commons.collections15.Transformer<A,B> |
getMapTransformer(Map<A,B> map)
Obtains the equivalent
Transformer of a Map . |
static <A> Set<A> |
intersect(Collection<A>... collections)
Returns the intersection set of a series of input collections.
|
static <A> String |
join(Collection<A> collection,
String separator)
Joins the elements in an input collection using a given separator.
|
static <A> List<A> |
listOf(A... values)
Returns a list of the comma-separated input values.
|
static <A,B> List<B> |
select(Map<A,B> map,
Collection<A> keys)
Returns a list of the values of the selected elements from an input map.
|
static <A,B> Map<A,B> |
selectEntries(Map<A,B> map,
Set<A> keys)
Returns a map of selected elements from the input map.
|
static <A> Set<A> |
setOf(A... values)
Returns a set of the comma-separated input values.
|
static <A extends Comparable<A>,B extends Comparable<B>> |
sort(Map<A,B> map,
Constants.OrderingType orderingType)
Returns a
SortedMap copy of the input map according to the values. |
static <K> SortedMap<K,Double> |
toMap(Collection<K> keys,
DoubleMatrix1D vals)
Given a collection of keys and an array of values, returns a map.
|
public static <A> boolean contains(Collection<A> collection, A value)
A
- Key typecollection
- Input collectionvalue
- Value to searchtrue
if value
is present in collection
, and false
otherwise. If collection
is empty, it will return false
public static <A> boolean containsAll(Collection<A> container, Collection<A> collection)
A
- Key typecontainer
- Container collectioncollection
- Collection with elements to be checkedtrue
if all elements in collection
are present in container
, and false
otherwise. If container
is empty, it will return false
public static <A> boolean containsAny(Collection<A> collection1, Collection<A> collection2)
A
- Key typecollection1
- Container collectioncollection2
- Collection with elements to be checkedtrue
if any element in collection
is present in container
, and false
otherwise. If container
is empty, it will return false
public static <A extends Number> Map<A,Integer> convertId2LinearIndexMap(Set<A> elements)
Returns a map containing the association of each element with its corresponding position within the set (in linear order). First element will be associate to index 0, second element to index 1, and so on.
Important: Since this method is widely used within the tool to deal with conversion between node/link/whatever identifier to its corresponding linear index (first element will be item 0, second element will be item 1, and so on), it is required that in iterator order the elements follow an asceding order.
A
- Key typeelements
- Set of elements in ascending iterator order (null values are not allowed)public static <A> Map<A,Integer> count(Collection<A> collection)
A
- Key typecollection
- Input collectionpublic static <A,B> List<A> find(Map<A,B> map, B value, Constants.SearchType searchType)
A
- Key typeB
- Value typemap
- Input mapvalue
- Value to be searched forsearchType
- Indicates whether the first, the last, or all occurrences are returnedpublic static <A,B> org.apache.commons.collections15.Transformer<A,B> getMapTransformer(Map<A,B> map)
Transformer
of a Map
.A
- Key typeB
- Value typemap
- Input mapTransformer
public static <A> Set<A> intersect(Collection<A>... collections)
A
- Key typecollections
- Series of input collectionspublic static <A> String join(Collection<A> collection, String separator)
A
- Key typecollection
- Input collectionseparator
- SeparatorString
representation of the input collection
public static <A> List<A> listOf(A... values)
A
- Value typevalues
- Comma-separated input valuespublic static <A,B> List<B> select(Map<A,B> map, Collection<A> keys)
A
- Key typeB
- Value typemap
- Input mapkeys
- Keys of the elements to be selectedpublic static <A,B> Map<A,B> selectEntries(Map<A,B> map, Set<A> keys)
A
- Key typeB
- Value typemap
- Input mapkeys
- Keys of the elements to be selected.public static <A> Set<A> setOf(A... values)
A
- Value typevalues
- Comma-separated input valuespublic static <A extends Comparable<A>,B extends Comparable<B>> SortedMap<A,B> sort(Map<A,B> map, Constants.OrderingType orderingType)
SortedMap
copy of the input map according to the values.A
- Key typeB
- Value typemap
- Input maporderingType
- Ascending or descendingSortedMap
. For keys sharing the same value, entries will be ordered according to ascending order of keys.public static <K> SortedMap<K,Double> toMap(Collection<K> keys, DoubleMatrix1D vals)
K
- Key typekeys
- Input keysvals
- InputCopyright © 2018. All rights reserved.