|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
ObjectStatic
XCollections
public final class XCollections
Static methods working on Collection objects. This is an extension to the
Java Collections utility class.
| utility/geotk-utility (download) | View source code for this class |
| Method Summary | ||
|---|---|---|
static
|
addIfNonNull(Collection<E> collection,
E element)
Adds the given element to the given collection only if the element is non-null. |
|
static Collection<?> |
asCollection(Object value)
Returns the given value as a collection. |
|
static void |
clear(Collection<?> collection)
Clears the given collection, if non-null. |
|
static void |
clear(Map<?,?> map)
Clears the given map, if non-null. |
|
static
|
collectionComparator()
Returns a comparator for arbitrary collections of comparable elements. |
|
static
|
copy(Collection<E> collection)
Copies the content of the given collection to a standard Java collection. |
|
static
|
copy(Map<K,V> map)
Copies the content of the given map to a standard Java map. |
|
static
|
emptyQueue()
Returns a queue which is always empty and accepts no element. |
|
static
|
emptySortedSet()
Returns a sorted set which is always empty and accepts no element. |
|
static int |
hashMapCapacity(int elements)
Returns the capacity to be given to the HashMap
constructor for holding the given number of elements. |
|
static
|
immutableSet(E... array)
Returns the specified array as an immutable set, or null if the array is null. |
|
static boolean |
isNullOrEmpty(Collection<?> collection)
Returns true if the given collection is either null or
empty. |
|
static boolean |
isNullOrEmpty(Map<?,?> map)
Returns true if the given map is either null or empty. |
|
static
|
listComparator()
Returns a comparator for lists of comparable elements. |
|
static
|
sortedSetComparator()
Returns a comparator for sorted sets of comparable elements. |
|
static
|
unmodifiableMap(Map<K,V> map)
Returns a unmodifiable version of the given map. |
|
| Methods inherited from class Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
|---|
public static void clear(Collection<?> collection)
collection - The collection to clear, or null.public static void clear(Map<?,?> map)
map - The map to clear, or null.public static boolean isNullOrEmpty(Collection<?> collection)
true if the given collection is either null or
empty. If this method returns false,
then the given collection is guaranteed to be non-null and to contain at least
one element.
collection - The collection to test, or null.
true if the given collection is null or empty, or false otherwise.public static boolean isNullOrEmpty(Map<?,?> map)
true if the given map is either null or empty.
If this method returns false, then the given map is guaranteed to be non-null and
to contain at least one element.
map - The map to test, or null.
true if the given map is null or empty, or false otherwise.
public static <E> boolean addIfNonNull(Collection<E> collection,
E element)
E - The type of elements in the collection.collection - The collection in which to add elements, or null.element - The element to add in the collection, or null.
true if the given element has been added, or false otherwise.public static <E> Queue<E> emptyQueue()
E - The type of elements in the empty collection.
Collections.emptyList(),
Collections.emptySet()public static <E> SortedSet<E> emptySortedSet()
E - The type of elements in the empty collection.
Collections.emptyList(),
Collections.emptySet()public static <E> Set<E> immutableSet(E... array)
null if the array is null.
If the given array contains duplicated elements, i.e. elements that are equal in the
sense of Object.equals(Object), then only the last instance of the duplicated
values will be included in the returned set.
E - The type of array elements.array - The array to copy in a set. May be null.
null if the given array was null.Collections.unmodifiableSet(Set)public static <K,V> Map<K,V> unmodifiableMap(Map<K,V> map)
Collections.unmodifiableMap(Map) in that it tries to returns a more
efficient object when there is zero or one element. The map returned by this
method may or may not be a view of the given map. Consequently this method
shall be used only if the given map will not be
modified after this method call. In case of doubt, use the standard
Collections.unmodifiableMap(Map) method instead.
K - The type of keys in the map.V - The type of values in the map.map - The map to make unmodifiable, or null.
null if the given map was null.public static Collection<?> asCollection(Object value)
Collection, then it is returned unchanged.
value - The value to return as a collection, or null.
null).public static <T extends Comparable<T>> Comparator<List<T>> listComparator()
If both lists have the same length and equal elements in the sense of
Comparable.compareTo(T), then the comparator returns 0.
T - The type of elements in both lists.
public static <T extends Comparable<T>> Comparator<SortedSet<T>> sortedSetComparator()
T - The type of elements in both sets.
public static <T extends Comparable<T>> Comparator<Collection<T>> collectionComparator()
TreeSet, LinkedHashSet or queues.
Do not use it with HashSet.
T - The type of elements in both collections.
public static int hashMapCapacity(int elements)
HashMap
constructor for holding the given number of elements. This method computes the capacity
for the default load factor, which is 0.75.
The same calculation can be used for LinkedHashMap and
HashSet as well, which are built on top of HashMap.
elements - The number of elements to be put into the hash map or hash set.
public static <E> Collection<E> copy(Collection<E> collection)
| Input type | Output type |
|---|---|
SortedSet | TreeSet |
HashSet | HashSet |
Other Set | LinkedHashSet |
Queue | LinkedList |
List or other Collection | ArrayList |
E - The type of elements in the collection.collection - The collection to copy, or null.
null if the given collection was null.public static <K,V> Map<K,V> copy(Map<K,V> map)
| Input type | Output type |
|---|---|
SortedMap | TreeMap |
HashMap | HashMap |
Other Map | LinkedHashMap |
K - The type of keys in the map.V - The type of values in the map.map - The map to copy, or null.
null if the given map was null.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||