public final class CollectionUtils extends Object
The CollectionUtils
class provides utility methods for
Collection
handling.
Modifier and Type | Method and Description |
---|---|
static <K,V> void |
accumulate(Map<K,List<V>> map,
K key,
V value)
Adds
value to the list associated with key in
map .If key has no value yet, a list is created fist. |
static <E> void |
addAll(Collection<? super E> collection,
E... elements)
Appends the specified
elements to the
collection . |
static int |
addIfNotInList(List<?> list,
List<?> elements)
Appends the specified
elements to the list if the
elements are not already present in the list. |
static <E> boolean |
addIfNotInList(List<E> list,
E element,
Comparer<E> comparer)
Appends the specified
element to the list if the
element is not already present in the list. |
static <E> int |
addIfNotInList(List<E> list,
List<E> elements,
Comparer<E> comparer)
Appends the specified
elements to the list if the
elements are not already present in the list.The comparer is used to compare the elements. |
static boolean |
addIfNotInList(List list,
Object element)
Appends the specified
element to the list if the
element is not already present in the list. |
static <T> Enumeration<T> |
asEnumeration(Iterator<T> e)
Returns an enumeration over the specified iterator.
|
static <T> Iterator<T> |
asIterator(Enumeration<T> e)
Returns an iterator over the specified enumeration.
|
static <T> Set<T> |
asLinkedSet(T... values)
Returns a set containing all of the elements in this
values . |
static <T> List<T> |
asList(Enumeration<T> e)
Returns an list containing all of the elements in this
e in
proper sequence (from first to last element). |
static <T> List<T> |
asList(Iterator<T> it)
Returns an list containing all of the elements in this
it in
proper sequence (from first to last element). |
static <T> List<T> |
asList(T... values)
Returns an list containing all of the elements in this
values in proper sequence (from first to last element). |
static <T> Set<T> |
asSet(T... values)
Returns a set containing all of the elements in this
values .It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. |
static <E> boolean |
contains(List<E> list,
E element,
Comparer<E> comparer)
Returns true if the
list contains the specified
element. |
static void |
copyIntoArray(Collection c,
int collectionOffset,
Object[] array,
int destOffset,
int count)
Copies an collection from the specified
c , beginning at the
specified position, to the specified position of the destination array. |
static void |
copyIntoArray(Collection c,
Object[] array,
int destOffset)
Copies collection elements into the given
array . |
static <E> List<E> |
createList(E value,
int count)
Create a
List with the size of count . |
static void |
deleteDoubleValues(List list)
Delete all double values of the
list . |
static void |
ensureCapacity(List<?> list,
int add)
Increases the capacity of this List instance by
add
. |
static int |
frequency(Collection<?> c,
Object search)
Returns the number of elements in the specified collection equal to the
specified object.
|
static boolean |
hasDoubleValues(List list)
Returns
true if the list has double elements. |
static <E> int |
indexOf(List<E> list,
E element,
Comparer<E> comparer)
Returns the index of the first occurrence of the specified element in the
list , or -1 if this list does not contain the element. |
static int |
indexOf(List list,
Object element,
int... skipIndices) |
static <T> int[] |
indicesOf(List<T> list,
T... obj)
Returns the indices of the occurrence of the specified elements in the
list . |
static void |
move(List list,
int index,
boolean up)
Move the element at the specified positions (
index ). |
static void |
move(List list,
int from,
int to)
Move the element at the specified positions (
from ) to the
specified position (to ) in the list . |
static void |
move(List list,
int firstIndex,
int lastIndex,
int to) |
static int |
remove(Map map,
Object value)
Removes the specified
value from the map , if
they are present (optional operation). |
static <E> int |
removeAll(Collection<? super E> list,
E... elements)
Removes the specified
elements from the list ,
if they are present (optional operation). |
static <E extends Enum<E>> |
sortByName(List<E> list) |
static void |
swap(List list,
int index1,
int index2)
Swaps the elements at the specified positions in the specified list.
|
static Map<String,Object> |
toMap(ResourceBundle bundle) |
public static void copyIntoArray(Collection c, Object[] array, int destOffset) throws IndexOutOfBoundsException, ArrayStoreException, NullPointerException
array
.
This method is a alias for
CollectionUtils.copyIntoArray(c,0,array,destOffset,c.size());
c
- the collection to copyarray
- the destination arraydestOffset
- starting position in the array
IndexOutOfBoundsException
- if copying would cause access of data outside array bounds.ArrayStoreException
- if an element in the c
could not be stored into
the array
because of a type mismatch.NullPointerException
- if either c
or array
is
null
.copyIntoArray(Collection, int, Object[], int, int)
public static void copyIntoArray(Collection c, int collectionOffset, Object[] array, int destOffset, int count) throws IndexOutOfBoundsException, ArrayStoreException, NullPointerException
c
, beginning at the
specified position, to the specified position of the destination array.c
- the collection to copycollectionOffset
- starting position in the source collectionarray
- the destination arraydestOffset
- starting position in the array
count
- the number of elements to be copiedIndexOutOfBoundsException
- if copying would cause access of data outside array bounds.ArrayStoreException
- if an element in the c
could not be stored into
the array
because of a type mismatch.NullPointerException
- if either c
or array
is
null
.System.arraycopy(Object, int, Object, int, int)
public static <T> List<T> asList(T... values)
values
in proper sequence (from first to last element).
The runtime type of the returned List
is that of the specified
T
.
T
- any typevalues
- an array of type T
elementsvalues
Arrays.asList(Object...)
public static <T> List<T> asList(Enumeration<T> e)
e
in
proper sequence (from first to last element).
The runtime type of the returned List
is that of the specified
T
.
T
- any typee
- an Enumeration
of type T
e
public static <T> List<T> asList(Iterator<T> it)
it
in
proper sequence (from first to last element).
The runtime type of the returned List
is that of the specified
T
.
T
- any typeit
- an Iterator
of type T
it
public static <T> Iterator<T> asIterator(Enumeration<T> e)
T
- any typee
- an Enumeration
of type T
Iterator
over the specified enumerationpublic static <T> Enumeration<T> asEnumeration(Iterator<T> e)
T
- any typee
- an Iterator
of type T
Enumeration
over the specified iteratorpublic static <E> List<E> createList(E value, int count)
List
with the size of count
. The
value
are added count
times.
The runtime type of the returned List
is that of the specified
E
.
E
- any typevalue
- the element to insert into the listcount
- the number of E
elements of the listvalue
public static <T> Set<T> asSet(T... values)
values
.
The runtime type of the returned Set
is that of the specified
T
.
public static <T> Set<T> asLinkedSet(T... values)
values
.
with predictable iteration order
The runtime type of the returned Set
is that of the specified
T
.
public static <E> void addAll(Collection<? super E> collection, E... elements)
elements
to the
collection
. The capacity of the collection
is
increased by the count of the elements
.collection
- the destination Collection
elements
- the elements to be placed in the collection
public static <E> int removeAll(Collection<? super E> list, E... elements)
elements
from the list
,
if they are present (optional operation).list
- the destination Collection
elements
- the elements to be removed from the list
Collection.removeAll(Collection)
public static int remove(Map map, Object value)
value
from the map
, if
they are present (optional operation).map
- the destination Map
value
- the element to be removed from the map
Object.equals(Object)
,
Iterator
public static void ensureCapacity(List<?> list, int add)
add
.list
- the List
to increase the sizeadd
- the additional capacitypublic static int addIfNotInList(List<?> list, List<?> elements)
elements
to the list if the
elements
are not already present in the list.list
- the destination List
elements
- the list of elements to addpublic static <E> int addIfNotInList(List<E> list, List<E> elements, Comparer<E> comparer)
elements
to the list if the
elements
are not already present in the list.comparer
is used to compare the elements.E
- any typelist
- the destination List
elements
- the list of elements to addcomparer
- the comparer by which the element are compared.int
public static boolean addIfNotInList(List list, Object element)
element
to the list if the
element
is not already present in the list.list
- the destination List
element
- a Object
to addtrue
if the element
is added, otherwise
flase
public static <E> boolean addIfNotInList(List<E> list, E element, Comparer<E> comparer)
element
to the list if the
element
is not already present in the list.E
- any typelist
- the destination List
element
- the element to be searched for.comparer
- the comparer by which the element are compared.true
if the element
is added,
false
if the element
is already present
in the list
public static <E> boolean contains(List<E> list, E element, Comparer<E> comparer) throws NullPointerException
list
contains the specified
element. The comparer
is used to compare the elements.E
- any typelist
- the destination List
element
- the element to be searched for.comparer
- the comparer by which the element are compared.true
if the list contains the element
,
otherwise false
NullPointerException
- if the comparer
is null
indexOf(List, Object, Comparer)
public static <E> int indexOf(List<E> list, E element, Comparer<E> comparer) throws NullPointerException
list
, or -1 if this list does not contain the element.E
- any typelist
- the list to be searched.element
- the element to be searched for.comparer
- the comparer by which the element are compared.-1
.NullPointerException
- if the list
or comparer
is
null
public static void deleteDoubleValues(List list)
list
.list
- the destination listhasDoubleValues(List)
public static boolean hasDoubleValues(List list)
true
if the list
has double elements.list
- the destination List
true
if the list
has double elements,
otherwise false
public static <T> int[] indicesOf(List<T> list, T... obj) throws NullPointerException
list
.T
- any typelist
- the destination List
obj
- the elements to searched forint
arrayNullPointerException
- if list
is null
public static int frequency(Collection<?> c, Object search) throws NullPointerException
c
- the collection in which to determine the frequency of
search
search
- the object whose frequency is to be determinedc
equal to the specified
object.NullPointerException
- if c
is nullpublic static void swap(List list, int index1, int index2) throws IndexOutOfBoundsException
list
- The list in which to swap elementsindex1
- the index of one element to be swappedindex2
- the index of the other element to be swappedIndexOutOfBoundsException
- if either index1 or index2 is out of range
(index1 < 0 || index1 >= list.size() || index2 < 0
|| index2 >= list.size()).public static void move(List list, int index, boolean up)
index
).list
- The list in which to move elementsindex
- the index of the element to be movedup
- define the elements new position; if true
the new
index is increased by 1, otherwise the the new index is
reduced by 1public static void move(List list, int from, int to)
from
) to the
specified position (to
) in the list
.list
- The list in which to move elementsfrom
- the index of the element to be movedto
- the index of the elements new positionpublic static void move(List list, int firstIndex, int lastIndex, int to) throws IndexOutOfBoundsException
IndexOutOfBoundsException
public static <K,V> void accumulate(Map<K,List<V>> map, K key, V value)
value
to the list associated with key
in
map
.key
has no value yet, a list is created fist.K
- The key's typeV
- The value's typemap
- The storekey
- The key associated with valuevalue
- The value to accumulatepublic static Map<String,Object> toMap(ResourceBundle bundle)
Copyright © 2003–2021 XDEV Software. All rights reserved.