public class CollectionUtil
extends java.lang.Object
Collection
s.Modifier and Type | Field and Description |
---|---|
static java.lang.String |
SEPARATOR
The default separator.
|
Modifier | Constructor and Description |
---|---|
private |
CollectionUtil()
Forbid instances by this private constructor.
|
Modifier and Type | Method and Description |
---|---|
static <T> void |
addAll(java.util.Collection<T> collection,
java.lang.Iterable<T> iterable)
Adds all elements to the
Collection . |
static <T> void |
addAll(java.util.Collection<T> collection,
T... elementsToAdd)
Adds all elements to the
Collection . |
static <T> java.util.List<T> |
arrayToList(T[] array)
Converts the given array to a new
List . |
static <T> void |
binaryInsert(java.util.List<T> list,
T toInsert,
java.util.Comparator<T> comparator)
Performs a binary insert on the given sorted
List . |
static <T> boolean |
contains(java.lang.Iterable<T> iterable,
T element)
Checks if the given element is contained in the given
Iterable . |
static <T> boolean |
containsSame(java.util.Collection<T> first,
java.util.Collection<T> second)
Checks if the given two
Collection s contains the same elements
in any order. |
static <T> int |
count(java.lang.Iterable<T> iterable,
IFilter<T> filter)
Counts the number of elements in the given
Iterable which
are selected by the given IFilter . |
static <T> T |
getFirst(java.lang.Iterable<T> iterable)
Returns the first element from the given
Iterable . |
static <T> int |
indexOf(java.util.Iterator<T> iter,
T toSearch)
Returns the index of the element to search in the given iterator.
|
static boolean |
isEmpty(java.util.Collection<?> collection)
Nullpointersave execution of
Collection.isEmpty() . |
static boolean |
isEmpty(java.util.Map<?,?> map)
Nullpointersave execution of
Map.isEmpty() . |
static <T> boolean |
removeAll(java.util.Collection<T> collection,
T... elementsToRemove)
Removes all elements from the
Collection . |
static <T> boolean |
removeComplete(java.util.Collection<T> collection,
T toRemove)
Removes all occurrences of the element in the given
Collection . |
static <T> T |
removeFirst(java.lang.Iterable<T> iterable)
Removes the first element from the given
Iterable . |
static <T> T |
search(java.lang.Iterable<T> iterable,
IFilter<T> filter)
Searches an element in the given
Iterable instance. |
static <T> java.util.List<T> |
searchAll(java.lang.Iterable<T> iterable,
IFilter<T> filter)
Searches all elements accepted by the given
IFilter . |
static <T> T |
searchAndRemove(java.lang.Iterable<T> iterable,
IFilter<T> filter)
Searches an element in the given
Iterable instance and removes
the found element from it. |
static <T,E extends java.lang.Throwable> |
searchAndRemoveWithException(java.lang.Iterable<T> iterable,
IFilterWithException<T,E> filter)
Searches an element in the given
Iterable instance and removes
the found element from it. |
static <T> java.util.List<T> |
toList(T... objects)
Converts the given objects into a
List . |
static <T> java.util.Set<T> |
toSet(T... objects)
Converts the given objects into a
Set . |
static java.lang.String |
toString(java.util.Collection<?> collection)
Converts the
Collection into a String . |
static java.lang.String |
toString(java.util.Collection<?> collection,
java.lang.String separator)
Converts the
Collection into a String and uses the
defined separator to separate elements. |
public static final java.lang.String SEPARATOR
private CollectionUtil()
public static <T> int indexOf(java.util.Iterator<T> iter, T toSearch)
iter
- The iterator to search in.toSearch
- The element to search.-1
if it was not found.public static java.lang.String toString(java.util.Collection<?> collection)
Collection
into a String
.collection
- The Collection
to convert.Collection
as String
.public static java.lang.String toString(java.util.Collection<?> collection, java.lang.String separator)
Collection
into a String
and uses the
defined separator to separate elements.collection
- The Collection
to convert.separator
- The separator between elements.Collection
as String
.public static boolean isEmpty(java.util.Collection<?> collection)
Collection.isEmpty()
.collection
- The given Collection
.true
= is empty or null
, false
= is not empty.public static boolean isEmpty(java.util.Map<?,?> map)
Map.isEmpty()
.map
- The given Map
.true
= is empty or null
, false
= is not empty.public static <T> java.util.List<T> toList(T... objects)
List
.T
- The type of the objects.objects
- The objects array to convert.List
.public static <T> java.util.Set<T> toSet(T... objects)
Set
.T
- The type of the objects.objects
- The objects array to convert.Set
.public static <T> void addAll(java.util.Collection<T> collection, T... elementsToAdd)
Collection
.T
- The type of the Collection
s elements.collection
- The Collection
to add to.elementsToAdd
- The elements to add.public static <T> void addAll(java.util.Collection<T> collection, java.lang.Iterable<T> iterable)
Collection
.T
- The type of the Collection
s elements.collection
- The Collection
to add to.elementsToAdd
- The elements to add.public static <T> boolean removeAll(java.util.Collection<T> collection, T... elementsToRemove)
Collection
.T
- The type of the Collection
s elements.collection
- The Collection
to remove from.elementsToRemove
- The elements to remove.true
if the Collection
changed as result of this call.public static <T> boolean removeComplete(java.util.Collection<T> collection, T toRemove)
Collection
.collection
- The Collection
to remove from.toRemove
- The element to remove.true
if at least one element was removed, false
if the Collection
was not modified.public static <T> java.util.List<T> searchAll(java.lang.Iterable<T> iterable, IFilter<T> filter)
IFilter
.public static <T> T search(java.lang.Iterable<T> iterable, IFilter<T> filter)
Iterable
instance.iterable
- The instance to search in.filter
- The filter to select an element.null
if no element was found.public static <T> T searchAndRemove(java.lang.Iterable<T> iterable, IFilter<T> filter)
Iterable
instance and removes
the found element from it.iterable
- The instance to search in.filter
- The filter to select an element.null
if no element was found.public static <T,E extends java.lang.Throwable> T searchAndRemoveWithException(java.lang.Iterable<T> iterable, IFilterWithException<T,E> filter) throws E extends java.lang.Throwable
Iterable
instance and removes
the found element from it.iterable
- The instance to search in.filter
- The filter to select an element.null
if no element was found.E extends java.lang.Throwable
public static <T> boolean contains(java.lang.Iterable<T> iterable, T element)
Iterable
.iterable
- The given Iterable
to search in.element
- The element to search.true
= contained, false
= not containedpublic static <T> int count(java.lang.Iterable<T> iterable, IFilter<T> filter)
Iterable
which
are selected by the given IFilter
.public static <T> boolean containsSame(java.util.Collection<T> first, java.util.Collection<T> second)
Checks if the given two Collection
s contains the same elements
in any order.
Empty Collection
s and null
parameters are treated as equal.
first
- The first Collection
.second
- The second Collection
.true
both Collection
s contains same elements, false
Collection
s are different.public static <T> T getFirst(java.lang.Iterable<T> iterable)
Iterable
.iterable
- The Iterable
to get first element from.null
if no element is available.public static <T> T removeFirst(java.lang.Iterable<T> iterable)
Iterable
.iterable
- The Iterable
to remove first element from.null
if no element was removed.public static <T> java.util.List<T> arrayToList(T[] array)
List
.array
- to be converted.List
containing all array elements.public static <T> void binaryInsert(java.util.List<T> list, T toInsert, java.util.Comparator<T> comparator)
List
.list
- The sorted List
to insert in.toInsert
- The element to insert.comparator
- The Comparator
to use.