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