public final class CollectionHelpers
extends java.lang.Object
Make use of "static imports" to omit even more needless code.
Modifier and Type | Method and Description |
---|---|
static <T> T[] |
arr(T... ts)
"Converts" the incoming "varargs" into an array.
|
static <E> java.util.List<E> |
arrList()
Creates an empty
ArrayList that uses a little cleverness with
Java's generics. |
static <E> java.util.List<E> |
arrList(java.util.Collection<? extends E> c)
Copies an existing
Collection into a new ArrayList . |
static <E> java.util.List<E> |
arrList(int capacity)
Creates an empty
ArrayList with a given capacity. |
static <A,B extends A> |
cast(A o)
"Generics-friendly" way to cast an object of some superclass
(
A ) to a subclass or implementation (B ). |
static <E> java.util.Collection<E> |
collect(java.lang.Class<? extends java.util.Collection> cl,
E... elements)
Creates a new
cl instance (limited to things implementing
Collection ) populated with the "varargs". |
static <E> java.util.List<E> |
concurrentList()
Creates an empty
CopyOnWriteArrayList . |
static <E> java.util.List<E> |
concurrentList(java.util.Collection<E> original)
Creates a new
CopyOnWriteArrayList that contains all of the
elements in original . |
static <E> java.util.List<E> |
concurrentList(E... elems)
Creates a new
CopyOnWriteArrayList from the incoming
"varargs". |
static <K,V> java.util.Map<K,V> |
concurrentMap()
Abuses Java's sad "type" implementation to create a new
ConcurrentHashMap . |
static <E> java.util.Set<E> |
concurrentSet()
Creates a new
CopyOnWriteArraySet . |
static <E> java.util.Set<E> |
concurrentSet(java.util.Collection<E> original)
Creates a new
CopyOnWriteArraySet that contains all of the
elements in original . |
static <E> java.util.Set<E> |
concurrentSet(E... elems)
Creates a new
CopyOnWriteArraySet from the incoming
"varargs". |
static boolean |
contains(java.lang.Object collection,
java.lang.Object item)
Searches an object to see if it "contains" another object.
|
static int |
len(java.lang.Object o)
Determines the "length" of a given object.
|
static <E> java.util.List<E> |
linkedList() |
static <E> java.util.List<E> |
linkedList(java.util.Collection<? extends E> c) |
static <E> java.util.List<E> |
list(E... elements)
Creates a
List from incoming "varargs". |
static <A,B> java.util.List<B> |
map(Function<A,B> f,
java.util.List<A> as)
Applies a given function to each item in a given list.
|
static <A,B> java.util.Set<B> |
map(Function<A,B> f,
java.util.Set<A> as)
Applies a given function to each item in a given
Set . |
static <E> java.util.Set<E> |
newHashSet()
Creates an empty
HashSet that uses a little cleverness with
Java's generics. |
static <E> java.util.Set<E> |
newHashSet(java.util.Collection<E> original)
Copies an existing
Collection into a new HashSet . |
static <E> java.util.Set<E> |
newHashSet(int initialCapacity)
Creates an empty
HashSet with a given initial capacity. |
static <K,V> java.util.Map<K,V> |
newLinkedHashMap()
Creates an empty
LinkedHashMap that uses a little cleverness with
Java's generics. |
static <K,V> java.util.Map<K,V> |
newLinkedHashMap(int initialCapacity)
Creates an empty
LinkedHashMap with a given initial capacity. |
static <K,V> java.util.Map<K,V> |
newLinkedHashMap(java.util.Map<K,V> original)
Copies an existing
Map into a new LinkedHashMap . |
static <E> java.util.Set<E> |
newLinkedHashSet()
Creates an empty
LinkedHashSet that uses a little cleverness
with Java's generics. |
static <E> java.util.Set<E> |
newLinkedHashSet(java.util.Collection<E> original)
Copies a
Collection into a new LinkedHashSet . |
static <E> java.util.Set<E> |
newLinkedHashSet(int initialCapacity)
Creates an empty
LinkedHashSet with a given initial capacity. |
static <K,V> java.util.Map<K,V> |
newMap()
Creates an empty
HashSet that uses a little cleverness with
Java's generics. |
static <K,V> java.util.Map<K,V> |
newMap(int initialCapacity)
Creates an empty
HashSet with a given initial capacity. |
static <K,V> java.util.Map<K,V> |
newMap(java.util.Map<K,V> original)
Copies an existing
Map into a new HashMap . |
static <E> java.util.Collection<E> |
recollect(java.lang.Class<? extends java.util.Collection> cl,
java.util.Collection<E> old)
Copies an existing
Collection into a new (non-abstract!)
Collection class. |
static <E> java.util.Set<E> |
set(E... elements)
Creates a
Set from incoming "varargs". |
static <K,V> java.util.Map<K,V> |
zipMap(java.util.Collection<? extends K> keys,
java.util.Collection<? extends V> values)
A version of
zipMap(Object[], Object[]) that works with
Collection s. |
static <K,V> java.util.Map<K,V> |
zipMap(K[] keys,
V[] values)
Takes arrays of
keys and values and merges them
together to form a Map . |
@SafeVarargs public static <T> T[] arr(T... ts)
Useful for doing things like:
String[] strs = arr("hello", "how", "are", "you?");
T
- Type of items to be converted into an array.ts
- Items that will make up the elements of the returned array.
Cannot be null
, and (for now) the items should be of the
same type.ts
.@SafeVarargs public static <E> java.util.List<E> list(E... elements)
List
from incoming "varargs". Currently
uses ArrayList
as the List
implementation.
Used like so:
List<String> listy = list("y", "helo", "thar");
E
- Type of items that will be added to the resulting collection.elements
- Items that will make up the elements of the returned
List
.List
whose elements are each item within
elements
.@SafeVarargs public static <E> java.util.Set<E> set(E... elements)
Set
from incoming "varargs". Currently uses
LinkedHashSet
as the Set
implementation (to preserve
ordering).
Used like so:
for (String s : set("beep", "boop", "blorp")) { ... }
E
- Type of items that will be added to the resulting collection.elements
- Items that will appear within the returned Set
.
Cannot be null
, and (for now) the items should be of the
same type.Set
containing the items in elements
. Remember
that Set
s only contain unique elements!public static <E> java.util.Collection<E> collect(java.lang.Class<? extends java.util.Collection> cl, E... elements)
cl
instance (limited to things implementing
Collection
) populated with the "varargs". Useful if
you truly despise arr(Object...)
, list(Object...)
,
or set(Object...)
.
Example: Collection<Integer> ints = collect(PriorityBlockingQueue.class, 1, 2, 3);
E
- Type of items that will be added to the resulting collection.cl
- A (non-abstract!) class that implements Collection
. Cannot be null
.elements
- Objects that will be added to the collection.cl
containing the given objects.java.lang.RuntimeException
- if Constructor#newInstance(Object...)
had problems.arr(Object...)
,
list(Object...)
,
set(Object...)
public static int len(java.lang.Object o)
Collection
Map
CharSequence
Array
Iterable
Iterator
More coming!
o
- Object
whose length we want. Cannot be null
.o
.java.lang.NullPointerException
- if o
is null
.java.lang.IllegalArgumentException
- if the method doesn't know how to test
whatever type of object o
might be.public static boolean contains(java.lang.Object collection, java.lang.Object item)
Collection
Map
CharSequence
Array
Iterable
Iterator
More coming!
collection
- Object
that will be searched for
item
. Cannot be null
.item
- Object
to search for within o
.
null
values are allowed.true
if o
contains item
, false
otherwise.java.lang.NullPointerException
- if o
is null
.java.lang.IllegalArgumentException
- if the method doesn't know how to
search whatever type of object o
might be.public static <E> java.util.Set<E> newHashSet()
HashSet
that uses a little cleverness with
Java's generics. Useful for eliminating redundant type information and
declaring fields as final
.
Please consider using newHashSet(int)
or
newHashSet(Collection)
instead of this method.
E
- Type of items that will be added to the resulting collection.HashSet
.newHashSet(int)
,
newHashSet(Collection)
public static <E> java.util.Set<E> newHashSet(int initialCapacity)
HashSet
with a given initial capacity.E
- Type of items that will be added to the resulting collection.initialCapacity
- Initial capacity of the HashSet
. Cannot
be negative.HashSet
with the given initial capacity.public static <E> java.util.Set<E> newHashSet(java.util.Collection<E> original)
Collection
into a new HashSet
.E
- Type of items that will be added to the resulting collection.original
- Collection
to be copied. Cannot be null
.HashSet
whose contents are the same as
original
.public static <E> java.util.Set<E> newLinkedHashSet()
LinkedHashSet
that uses a little cleverness
with Java's generics. Useful for eliminating redundant type
information and declaring fields as final
.
Please consider using newLinkedHashSet(int)
or
newLinkedHashSet(Collection)
instead of this method.
E
- Type of items that will be added to the resulting collection.LinkedHashSet
.newLinkedHashSet(int)
,
newLinkedHashSet(Collection)
public static <E> java.util.Set<E> newLinkedHashSet(int initialCapacity)
LinkedHashSet
with a given initial capacity.E
- Type of items that will be added to the resulting collection.initialCapacity
- Initial capacity of the LinkedHashSet
.
Cannot be negative.LinkedHashSet
with the given initial
capacity.public static <E> java.util.Set<E> newLinkedHashSet(java.util.Collection<E> original)
Collection
into a new LinkedHashSet
.E
- Type of items that will be added to the resulting collection.original
- Collection to be copied. Cannot be null
.LinkedHashSet
whose contents are the same as
original
.public static <K,V> java.util.Map<K,V> newMap()
HashSet
that uses a little cleverness with
Java's generics. Useful for eliminating redundant type information and
declaring fields as final
, while also reducing compiler warnings.
Please consider using newMap(int)
or
newMap(Map)
instead of this method.
K
- Type of keys that will be added to the Map
.V
- Type of values that will be added to the Map
.HashMap
.newMap(int)
,
newMap(Map)
public static <K,V> java.util.Map<K,V> newMap(int initialCapacity)
HashSet
with a given initial capacity.K
- Type of keys that will be added to the Map
.V
- Type of values that will be added to the Map
.initialCapacity
- Initial capacity of the HashMap
.
Cannot be negative.HashMap
with the given initial capacity.public static <K,V> java.util.Map<K,V> newMap(java.util.Map<K,V> original)
Map
into a new HashMap
.K
- Type of keys that will be added to the Map
.V
- Type of values that will be added to the Map
.original
- Map to be copied. Cannot be null
.HashMap
whose contents are the same as
original
.public static <K,V> java.util.Map<K,V> newLinkedHashMap()
LinkedHashMap
that uses a little cleverness with
Java's generics. Useful for eliminating redundant type information and
declaring fields as final
, while also reducing compiler warnings.
Please consider using newLinkedHashMap(int)
or
newLinkedHashSet(Collection)
instead of this method.
K
- Type of keys that will be added to the Map
.V
- Type of values that will be added to the Map
.LinkedHashMap
.newLinkedHashMap(int)
,
newLinkedHashMap(Map)
public static <K,V> java.util.Map<K,V> newLinkedHashMap(int initialCapacity)
LinkedHashMap
with a given initial capacity.K
- Type of keys that will be added to the Map
.V
- Type of values that will be added to the Map
.initialCapacity
- Initial capacity of the LinkedHashMap
.
Cannot be negative.LinkedHashMap
with the given initial
capacity.public static <K,V> java.util.Map<K,V> newLinkedHashMap(java.util.Map<K,V> original)
Map
into a new LinkedHashMap
.K
- Type of keys that will be added to the Map
.V
- Type of values that will be added to the Map
.original
- Map to be copied. Cannot be null
.LinkedHashMap
whose contents are the same as
original
.public static <K,V> java.util.Map<K,V> concurrentMap()
ConcurrentHashMap
.K
- Type of keys that will be added to the Map
.V
- Type of values that will be added to the Map
.ConcurrentHashMap
public static <E> java.util.List<E> concurrentList()
CopyOnWriteArrayList
. Keep in mind that you
only want to use CopyOnWriteArrayList
for lists that are not
going to be modified very often!E
- Type of items that will be added to the resulting collection.CopyOnWriteArrayList
.public static <E> java.util.List<E> concurrentList(java.util.Collection<E> original)
CopyOnWriteArrayList
that contains all of the
elements in original
. Keep in mind that you only want to use
CopyOnWriteArrayList
for lists that are not going to be
modified very often!E
- Type of items that will be added to the resulting collection.original
- Collection to be copied into the new list.CopyOnWriteArrayList
whose contents are the same
as original
.@SafeVarargs public static <E> java.util.List<E> concurrentList(E... elems)
CopyOnWriteArrayList
from the incoming
"varargs". Keep in mind that you only want to use
CopyOnWriteArrayList
for lists that are not going to be modified
very often!E
- Type of items that will be added to the resulting collection.elems
- Elements that will be contained in the resulting list.CopyOnWriteArrayList
that contains the incoming
objects.public static <E> java.util.Set<E> concurrentSet()
CopyOnWriteArraySet
. Keep in mind that you only
want to use a CopyOnWriteArraySet
for sets that are not going to
be modified very often!E
- Type of items that will be added to the resulting collection.CopyOnWriteArraySet
.public static <E> java.util.Set<E> concurrentSet(java.util.Collection<E> original)
CopyOnWriteArraySet
that contains all of the
elements in original
. Keep in mind that you only want to use a
CopyOnWriteArraySet
for sets that are not going to be modified
very often!E
- Type of items that will be added to the resulting collection.original
- Collection to be copied into the new set.CopyOnWriteArraySet
whose contents are the same as
original
.@SafeVarargs public static <E> java.util.Set<E> concurrentSet(E... elems)
CopyOnWriteArraySet
from the incoming
"varargs". Keep in mind that you only want to use a
CopyOnWriteArraySet
for sets that are not going to be modified
very often!E
- Type of items that will be added to the resulting collection.elems
- Elements that will be contained in the resulting set.CopyOnWriteArraySet
that contains the incoming objects.public static <E> java.util.List<E> linkedList()
public static <E> java.util.List<E> linkedList(java.util.Collection<? extends E> c)
public static <E> java.util.List<E> arrList()
ArrayList
that uses a little cleverness with
Java's generics. Useful for eliminating redundant type information and
declaring fields as final
.
Used like so:
List<String> listy = arrList();
Please consider using arrList(int)
or
arrList(Collection)
instead of this method.
E
- Type of items that will be added to the resulting collection.ArrayList
.arrList(int)
,
arrList(Collection)
public static <E> java.util.List<E> arrList(int capacity)
ArrayList
with a given capacity.E
- Type of items that will be added to the resulting collection.capacity
- The initial size of the returned ArrayList
.ArrayList
that has an initial capacity of
capacity
elements.ArrayList(int)
public static <E> java.util.List<E> arrList(java.util.Collection<? extends E> c)
Collection
into a new ArrayList
.E
- Type of items that will be added to the resulting collection.c
- Collection
whose elements are to be placed into the
returned ArrayList
.ArrayList
containing the elements of c
.ArrayList(Collection)
public static <E> java.util.Collection<E> recollect(java.lang.Class<? extends java.util.Collection> cl, java.util.Collection<E> old)
Collection
into a new (non-abstract!)
Collection
class.E
- Type of items that will be added to the resulting collection.cl
- Non-abstract Collection
class.old
- An existing Collection
.cl
that contains all of the elements
from old
.java.lang.RuntimeException
- if there was trouble creating a new instance
of cl
.collect(Class, Object...)
public static <K,V> java.util.Map<K,V> zipMap(K[] keys, V[] values)
keys
and values
and merges them
together to form a Map
. The returned Map
is a
LinkedHashMap
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.
K
- Type of keys that will be added to the Map
.V
- Type of values that will be added to the Map
.keys
- Array whose elements will be the keys in a Map
.values
- Array whose elements will the values in a Map
.Map
whose entries are of the form
keys[N], values[N]
.arr(Object...)
,
zipMap(Collection, Collection)
public static <K,V> java.util.Map<K,V> zipMap(java.util.Collection<? extends K> keys, java.util.Collection<? extends V> values)
zipMap(Object[], Object[])
that works with
Collection
s.K
- Type of keys that will be added to the Map
.V
- Type of values that will be added to the Map
.keys
- Items that will be the keys in the resulting Map
.values
- Items that will be the values in the result Map
.Map
whose entries are of the form
keys[N], values[N]
.zipMap(Object[], Object[])
public static <A,B> java.util.List<B> map(Function<A,B> f, java.util.List<A> as)
A
- Type of items that will be passed into f
.B
- Type of items that will be in the resulting List
.f
- The Function
to apply.as
- The list whose items are to be fed into f
.as
being passed through f
.public static <A,B> java.util.Set<B> map(Function<A,B> f, java.util.Set<A> as)
Set
.A
- Type of items that will be passed into f
.B
- Type of items that will be in the resulting Set
.f
- The Function
to apply to as
.as
- The Set
whose items are to be fed into f
.Set
containing the results of passing each element
in as
through f
.public static <A,B extends A> B cast(A o)
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 type
A
.
Example/Justification:
Consider a method like XmlUtil.findChildren(Node, String)
.
Despite findChildren
only returning lists containing Node
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 of cast
and findChildren
):
import static ucar.unidata.xml.XmlUtil.findChildren; import static edu.wisc.ssec.mcidasv.util.CollectionHelpers.cast; .... List<Node> nodes = cast(findChildren(panel, "blah"));
A
- Superclass of B
. This is what you are
"casting from"...likely Object
in most casesB
- Subclass of A
. This is what you are
"casting to".o
- The object whose type you are casting.o
, casted from type A
to B
. Enjoy!