Class CollectionHelpers
Make use of "static imports" to omit even more needless code.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T[]
arr
(T... ts) "Converts" the incoming "varargs" into an array.static <E> List
<E> arrList()
Creates an emptyArrayList
that uses a little cleverness with Java's generics.static <E> List
<E> arrList
(int capacity) Creates an emptyArrayList
with a given capacity.static <E> List
<E> arrList
(Collection<? extends E> c) Copies an existingCollection
into a newArrayList
.static <A,
B extends A>
Bcast
(A o) "Generics-friendly" way to cast an object of some superclass (A
) to a subclass or implementation (B
).static <E> Collection
<E> collect
(Class<? extends Collection> cl, E... elements) Creates a newcl
instance (limited to things implementingCollection
) populated with the "varargs".static <E> List
<E> Creates an emptyCopyOnWriteArrayList
.static <E> List
<E> concurrentList
(E... elems) Creates a newCopyOnWriteArrayList
from the incoming "varargs".static <E> List
<E> concurrentList
(Collection<E> original) Creates a newCopyOnWriteArrayList
that contains all of the elements inoriginal
.static <K,
V> Map <K, V> Abuses Java's sad "type" implementation to create a newConcurrentHashMap
.static <E> Set
<E> Creates a newCopyOnWriteArraySet
.static <E> Set
<E> concurrentSet
(E... elems) Creates a newCopyOnWriteArraySet
from the incoming "varargs".static <E> Set
<E> concurrentSet
(Collection<E> original) Creates a newCopyOnWriteArraySet
that contains all of the elements inoriginal
.static boolean
Searches an object to see if it "contains" another object.static int
Determines the "length" of a given object.static <E> List
<E> static <E> List
<E> linkedList
(Collection<? extends E> c) static <E> List
<E> list
(E... elements) Creates aList
from incoming "varargs".static <A,
B> List <B> Applies a given function to each item in a given list.static <A,
B> Set <B> Applies a given function to each item in a givenSet
.static <E> Set
<E> Creates an emptyHashSet
that uses a little cleverness with Java's generics.static <E> Set
<E> newHashSet
(int initialCapacity) Creates an emptyHashSet
with a given initial capacity.static <E> Set
<E> newHashSet
(Collection<E> original) Copies an existingCollection
into a newHashSet
.static <K,
V> Map <K, V> Creates an emptyLinkedHashMap
that uses a little cleverness with Java's generics.static <K,
V> Map <K, V> newLinkedHashMap
(int initialCapacity) Creates an emptyLinkedHashMap
with a given initial capacity.static <K,
V> Map <K, V> newLinkedHashMap
(Map<K, V> original) Copies an existingMap
into a newLinkedHashMap
.static <E> Set
<E> Creates an emptyLinkedHashSet
that uses a little cleverness with Java's generics.static <E> Set
<E> newLinkedHashSet
(int initialCapacity) Creates an emptyLinkedHashSet
with a given initial capacity.static <E> Set
<E> newLinkedHashSet
(Collection<E> original) Copies aCollection
into a newLinkedHashSet
.static <K,
V> Map <K, V> newMap()
Creates an emptyHashSet
that uses a little cleverness with Java's generics.static <K,
V> Map <K, V> newMap
(int initialCapacity) Creates an emptyHashSet
with a given initial capacity.static <K,
V> Map <K, V> static <E> Collection
<E> recollect
(Class<? extends Collection> cl, Collection<E> old) Copies an existingCollection
into a new (non-abstract!)static <E> Set
<E> set
(E... elements) Creates aSet
from incoming "varargs".static <K,
V> Map <K, V> zipMap
(Collection<? extends K> keys, Collection<? extends V> values) A version ofzipMap(Object[], Object[])
that works withCollection
s.static <K,
V> Map <K, V> zipMap
(K[] keys, V[] values)
-
Constructor Details
-
CollectionHelpers
private CollectionHelpers()Never!
-
-
Method Details
-
arr
"Converts" the incoming "varargs" into an array.Useful for doing things like:
String[] strs = arr("hello", "how", "are", "you?");
- Type Parameters:
T
- Type of items to be converted into an array.- Parameters:
ts
- Items that will make up the elements of the returned array. Cannot benull
, and (for now) the items should be of the same type.- Returns:
- Array populated with each item from
ts
.
-
list
Creates aList
from incoming "varargs". Currently usesArrayList
as theList
implementation.Used like so:
List<String> listy = list("y", "helo", "thar");
- Type Parameters:
E
- Type of items that will be added to the resulting collection.- Parameters:
elements
- Items that will make up the elements of the returnedList
.- Returns:
List
whose elements are each item withinelements
.
-
set
Creates aSet
from incoming "varargs". Currently usesLinkedHashSet
as theSet
implementation (to preserve ordering).Used like so:
for (String s : set("beep", "boop", "blorp")) { ... }
- Type Parameters:
E
- Type of items that will be added to the resulting collection.- Parameters:
elements
- Items that will appear within the returnedSet
. Cannot benull
, and (for now) the items should be of the same type.- Returns:
- A
Set
containing the items inelements
. Remember thatSet
s only contain unique elements!
-
collect
Creates a newcl
instance (limited to things implementingCollection
) populated with the "varargs". Useful if you truly despisearr(Object...)
,list(Object...)
, orset(Object...)
.Example:
Collection<Integer> ints = collect(PriorityBlockingQueue.class, 1, 2, 3);
- Type Parameters:
E
- Type of items that will be added to the resulting collection.- Parameters:
cl
- A (non-abstract!) class that implementsCollection
. Cannot benull
.elements
- Objects that will be added to the collection.- Returns:
- An instance of
cl
containing the given objects. - Throws:
RuntimeException
- ifConstructor#newInstance(Object...)
had problems.- See Also:
-
len
Determines the "length" of a given object. This method currently understands:More coming!
- Parameters:
o
-Object
whose length we want. Cannot benull
.- Returns:
- "Length" of
o
. - Throws:
NullPointerException
- ifo
isnull
.IllegalArgumentException
- if the method doesn't know how to test whatever type of objecto
might be.
-
contains
Searches an object to see if it "contains" another object. This method currently knows how to search:More coming!
- Parameters:
collection
-Object
that will be searched foritem
. Cannot benull
.item
-Object
to search for withino
.null
values are allowed.- Returns:
true
ifo
containsitem
,false
otherwise.- Throws:
NullPointerException
- ifo
isnull
.IllegalArgumentException
- if the method doesn't know how to search whatever type of objecto
might be.
-
newHashSet
Creates an emptyHashSet
that uses a little cleverness with Java's generics. Useful for eliminating redundant type information and declaring fields asfinal
.Please consider using
newHashSet(int)
ornewHashSet(Collection)
instead of this method.- Type Parameters:
E
- Type of items that will be added to the resulting collection.- Returns:
- A new, empty
HashSet
. - See Also:
-
newHashSet
Creates an emptyHashSet
with a given initial capacity.- Type Parameters:
E
- Type of items that will be added to the resulting collection.- Parameters:
initialCapacity
- Initial capacity of theHashSet
. Cannot be negative.- Returns:
- A new, empty
HashSet
with the given initial capacity.
-
newHashSet
Copies an existingCollection
into a newHashSet
.- Type Parameters:
E
- Type of items that will be added to the resulting collection.- Parameters:
original
-Collection
to be copied. Cannot benull
.- Returns:
- A new
HashSet
whose contents are the same asoriginal
.
-
newLinkedHashSet
Creates an emptyLinkedHashSet
that uses a little cleverness with Java's generics. Useful for eliminating redundant type information and declaring fields asfinal
.Please consider using
newLinkedHashSet(int)
ornewLinkedHashSet(Collection)
instead of this method.- Type Parameters:
E
- Type of items that will be added to the resulting collection.- Returns:
- A new, empty
LinkedHashSet
. - See Also:
-
newLinkedHashSet
Creates an emptyLinkedHashSet
with a given initial capacity.- Type Parameters:
E
- Type of items that will be added to the resulting collection.- Parameters:
initialCapacity
- Initial capacity of theLinkedHashSet
. Cannot be negative.- Returns:
- A new, empty
LinkedHashSet
with the given initial capacity.
-
newLinkedHashSet
Copies aCollection
into a newLinkedHashSet
.- Type Parameters:
E
- Type of items that will be added to the resulting collection.- Parameters:
original
- Collection to be copied. Cannot benull
.- Returns:
- A new
LinkedHashSet
whose contents are the same asoriginal
.
-
newMap
Creates an emptyHashSet
that uses a little cleverness with Java's generics. Useful for eliminating redundant type information and declaring fields asfinal
, while also reducing compiler warnings.Please consider using
newMap(int)
ornewMap(Map)
instead of this method.- Type Parameters:
K
- Type of keys that will be added to theMap
.V
- Type of values that will be added to theMap
.- Returns:
- A new, empty
HashMap
. - See Also:
-
newMap
Creates an emptyHashSet
with a given initial capacity.- Type Parameters:
K
- Type of keys that will be added to theMap
.V
- Type of values that will be added to theMap
.- Parameters:
initialCapacity
- Initial capacity of theHashMap
. Cannot be negative.- Returns:
- A new, empty
HashMap
with the given initial capacity.
-
newMap
- Type Parameters:
K
- Type of keys that will be added to theMap
.V
- Type of values that will be added to theMap
.- Parameters:
original
- Map to be copied. Cannot benull
.- Returns:
- A new
HashMap
whose contents are the same asoriginal
.
-
newLinkedHashMap
Creates an emptyLinkedHashMap
that uses a little cleverness with Java's generics. Useful for eliminating redundant type information and declaring fields asfinal
, while also reducing compiler warnings.Please consider using
newLinkedHashMap(int)
ornewLinkedHashSet(Collection)
instead of this method.- Type Parameters:
K
- Type of keys that will be added to theMap
.V
- Type of values that will be added to theMap
.- Returns:
- A new, empty
LinkedHashMap
. - See Also:
-
newLinkedHashMap
Creates an emptyLinkedHashMap
with a given initial capacity.- Type Parameters:
K
- Type of keys that will be added to theMap
.V
- Type of values that will be added to theMap
.- Parameters:
initialCapacity
- Initial capacity of theLinkedHashMap
. Cannot be negative.- Returns:
- A new, empty
LinkedHashMap
with the given initial capacity.
-
newLinkedHashMap
Copies an existingMap
into a newLinkedHashMap
.- Type Parameters:
K
- Type of keys that will be added to theMap
.V
- Type of values that will be added to theMap
.- Parameters:
original
- Map to be copied. Cannot benull
.- Returns:
- A new
LinkedHashMap
whose contents are the same asoriginal
.
-
concurrentMap
Abuses Java's sad "type" implementation to create a newConcurrentHashMap
.- Type Parameters:
K
- Type of keys that will be added to theMap
.V
- Type of values that will be added to theMap
.- Returns:
- Shiny and new
ConcurrentHashMap
-
concurrentList
Creates an emptyCopyOnWriteArrayList
. Keep in mind that you only want to useCopyOnWriteArrayList
for lists that are not going to be modified very often!- Type Parameters:
E
- Type of items that will be added to the resulting collection.- Returns:
- A new, empty
CopyOnWriteArrayList
.
-
concurrentList
Creates a newCopyOnWriteArrayList
that contains all of the elements inoriginal
. Keep in mind that you only want to useCopyOnWriteArrayList
for lists that are not going to be modified very often!- Type Parameters:
E
- Type of items that will be added to the resulting collection.- Parameters:
original
- Collection to be copied into the new list.- Returns:
- A new
CopyOnWriteArrayList
whose contents are the same asoriginal
.
-
concurrentList
Creates a newCopyOnWriteArrayList
from the incoming "varargs". Keep in mind that you only want to useCopyOnWriteArrayList
for lists that are not going to be modified very often!- Type Parameters:
E
- Type of items that will be added to the resulting collection.- Parameters:
elems
- Elements that will be contained in the resulting list.- Returns:
- A new
CopyOnWriteArrayList
that contains the incoming objects.
-
concurrentSet
Creates a newCopyOnWriteArraySet
. Keep in mind that you only want to use aCopyOnWriteArraySet
for sets that are not going to be modified very often!- Type Parameters:
E
- Type of items that will be added to the resulting collection.- Returns:
- New, empty
CopyOnWriteArraySet
.
-
concurrentSet
Creates a newCopyOnWriteArraySet
that contains all of the elements inoriginal
. Keep in mind that you only want to use aCopyOnWriteArraySet
for sets that are not going to be modified very often!- Type Parameters:
E
- Type of items that will be added to the resulting collection.- Parameters:
original
- Collection to be copied into the new set.- Returns:
CopyOnWriteArraySet
whose contents are the same asoriginal
.
-
concurrentSet
Creates a newCopyOnWriteArraySet
from the incoming "varargs". Keep in mind that you only want to use aCopyOnWriteArraySet
for sets that are not going to be modified very often!- Type Parameters:
E
- Type of items that will be added to the resulting collection.- Parameters:
elems
- Elements that will be contained in the resulting set.- Returns:
CopyOnWriteArraySet
that contains the incoming objects.
-
linkedList
-
linkedList
-
arrList
Creates an emptyArrayList
that uses a little cleverness with Java's generics. Useful for eliminating redundant type information and declaring fields asfinal
.Used like so:
List<String> listy = arrList();
Please consider using
arrList(int)
orarrList(Collection)
instead of this method.- Type Parameters:
E
- Type of items that will be added to the resulting collection.- Returns:
- A new, empty
ArrayList
. - See Also:
-
arrList
Creates an emptyArrayList
with a given capacity.- Type Parameters:
E
- Type of items that will be added to the resulting collection.- Parameters:
capacity
- The initial size of the returnedArrayList
.- Returns:
- A new, empty
ArrayList
that has an initial capacity ofcapacity
elements. - See Also:
-
arrList
Copies an existingCollection
into a newArrayList
.- Type Parameters:
E
- Type of items that will be added to the resulting collection.- Parameters:
c
-Collection
whose elements are to be placed into the returnedArrayList
.- Returns:
- An
ArrayList
containing the elements ofc
. - See Also:
-
recollect
Copies an existingCollection
into a new (non-abstract!)Collection
class.- Type Parameters:
E
- Type of items that will be added to the resulting collection.- Parameters:
cl
- Non-abstractCollection
class.old
- An existingCollection
.- Returns:
- A new instance of
cl
that contains all of the elements fromold
. - Throws:
RuntimeException
- if there was trouble creating a new instance ofcl
.- See Also:
-
zipMap
Takes arrays ofkeys
andvalues
and merges them together to form aMap
. The returnedMap
is aLinkedHashMap
and is truncated in length to the length of the shorter parameter.This is intended for use as "varargs" supplied to
arr(Object...)
. Rather than doing something ugly like:Map<String, String> mappy = new LinkedHashMap<String, String>(); mappy.put("key0", "val0"); mappy.put("key1", "val1"); ... mappy.put("keyN", "valN");
Simply do like so:mappy = zipMap( arr("key0", "key1", ..., "keyN"), arr("val0", "val1", ..., "valN"));
The latter approach also allows you to make
static final
Map
s much more easily.- Type Parameters:
K
- Type of keys that will be added to theMap
.V
- Type of values that will be added to theMap
.- Parameters:
keys
- Array whose elements will be the keys in aMap
.values
- Array whose elements will the values in aMap
.- Returns:
- A
Map
whose entries are of the formkeys[N], values[N]
. - See Also:
-
zipMap
A version ofzipMap(Object[], Object[])
that works withCollection
s.- Type Parameters:
K
- Type of keys that will be added to theMap
.V
- Type of values that will be added to theMap
.- Parameters:
keys
- Items that will be the keys in the resultingMap
.values
- Items that will be the values in the resultMap
.- Returns:
- A
Map
whose entries are of the formkeys[N], values[N]
. - See Also:
-
map
Applies a given function to each item in a given list.- Type Parameters:
A
- Type of items that will be passed intof
.B
- Type of items that will be in the resultingList
.- Parameters:
f
- TheFunction
to apply.as
- The list whose items are to be fed intof
.- Returns:
- New list containing the results of each element of
as
being passed throughf
.
-
map
Applies a given function to each item in a givenSet
.- Type Parameters:
A
- Type of items that will be passed intof
.B
- Type of items that will be in the resultingSet
.- Parameters:
f
- TheFunction
to apply toas
.as
- TheSet
whose items are to be fed intof
.- Returns:
- New
Set
containing the results of passing each element inas
throughf
.
-
cast
"Generics-friendly" way to cast an object of some superclass (A
) to a subclass or implementation (B
). This method will fail if you attempt to cast to a type that is not a subclass of typeA
.Example/Justification:
Consider a method likeXmlUtil.findChildren(Node, String)
.
DespitefindChildren
only returning lists containingNode
objects, Java will generate a warning for the following code:import ucar.unidata.xml.XmlUtil; .... List<Node> nodes = XmlUtil.findChildren(panel, "blah");
cast
is a nice and terse way to avoid those warnings. Here's the previous example (with static imports ofcast
andfindChildren
):import static ucar.unidata.xml.XmlUtil.findChildren; import static edu.wisc.ssec.mcidasv.util.CollectionHelpers.cast; .... List<Node> nodes = cast(findChildren(panel, "blah"));
- Type Parameters:
A
- Superclass ofB
. This is what you are "casting from"...likelyObject
in most casesB
- Subclass ofA
. This is what you are "casting to".- Parameters:
o
- The object whose type you are casting.- Returns:
o
, casted from typeA
toB
. Enjoy!
-