java.lang.Object | |
↳ | org.apache.commons.collections.ListUtils |
Provides utility methods and decorators for List instances.
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
EMPTY_LIST | An empty unmodifiable list. |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
ListUtils should not normally be instantiated. |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Returns a fixed-sized list backed by the given list.
| |||||||||||
Generates a hash code using the algorithm specified in
hashCode() . | |||||||||||
Returns a new list containing all elements that are contained in
both given lists.
| |||||||||||
Tests two lists for value-equality as per the equality contract in
equals(java.lang.Object) . | |||||||||||
Returns a "lazy" list whose elements will be created on demand.
| |||||||||||
Returns a predicated (validating) list backed by the given list.
| |||||||||||
Removes the elements in
remove from collection . | |||||||||||
Returns a List containing all the elements in
collection
that are also in retain . | |||||||||||
Subtracts all elements in the second list from the first list,
placing the results in a new list.
| |||||||||||
Returns the sum of the given lists.
| |||||||||||
Returns a synchronized list backed by the given list.
| |||||||||||
Returns a transformed list backed by the given list.
| |||||||||||
Returns a typed list backed by the given list.
| |||||||||||
Returns a new list containing the second list appended to the
first list.
| |||||||||||
Returns an unmodifiable list backed by the given list.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
An empty unmodifiable list. This uses the Collections Collections implementation and is provided for completeness.
ListUtils
should not normally be instantiated.
Returns a fixed-sized list backed by the given list.
Elements may not be added or removed from the returned list, but
existing elements can be changed (for instance, via the
set(int, Object)
method).
list | the list whose size to fix, must not be null |
---|
IllegalArgumentException | if the List is null |
---|
Generates a hash code using the algorithm specified in
hashCode()
.
This method is useful for implementing List
when you cannot
extend AbstractList. The method takes Collection instances to enable other
collection types to use the List implementation algorithm.
list | the list to generate the hashCode for, may be null |
---|
Returns a new list containing all elements that are contained in both given lists.
list1 | the first list |
---|---|
list2 | the second list |
NullPointerException | if either list is null |
---|
Tests two lists for value-equality as per the equality contract in
equals(java.lang.Object)
.
This method is useful for implementing List
when you cannot
extend AbstractList. The method takes Collection instances to enable other
collection types to use the List implementation algorithm.
The relevant text (slightly paraphrased as this is a static method) is:
Compares the two list objects for equality. Returns true if and only if both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.Note: The behaviour of this method is undefined if the lists are modified during the equals comparison.
list1 | the first list, may be null |
---|---|
list2 | the second list, may be null |
Returns a "lazy" list whose elements will be created on demand.
When the index passed to the returned list's get
method is greater than the list's size, then the factory will be used
to create a new object and that object will be inserted at that index.
For instance:
Factory factory = new Factory() { public Object create() { return new Date(); } } List lazy = ListUtils.lazyList(new ArrayList(), factory); Object obj = lazy.get(3);After the above code is executed,
obj
will contain
a new Date
instance. Furthermore, that Date
instance is the fourth element in the list. The first, second,
and third element are all set to null
.list | the list to make lazy, must not be null |
---|---|
factory | the factory for creating new objects, must not be null |
IllegalArgumentException | if the List or Factory is null |
---|
Returns a predicated (validating) list backed by the given list.
Only objects that pass the test in the given predicate can be added to the list. Trying to add an invalid object results in an IllegalArgumentException. It is important not to use the original list after invoking this method, as it is a backdoor for adding invalid objects.
list | the list to predicate, must not be null |
---|---|
predicate | the predicate for the list, must not be null |
IllegalArgumentException | if the List or Predicate is null |
---|
Removes the elements in remove
from collection
. That is, this
method returns a list containing all the elements in c
that are not in remove
. The cardinality of an element e
in the returned collection is the same as the cardinality of e
in collection
unless remove
contains e
, in which
case the cardinality is zero. This method is useful if you do not wish to modify
collection
and thus cannot call collection.removeAll(remove);
.
collection | the collection from which items are removed (in the returned collection) |
---|---|
remove | the items to be removed from the returned collection |
List
containing all the elements of c
except
any elements that also occur in remove
.NullPointerException | if either parameter is null |
---|
Returns a List containing all the elements in collection
that are also in retain
. The cardinality of an element e
in the returned list is the same as the cardinality of e
in collection
unless retain
does not contain e
, in which
case the cardinality is zero. This method is useful if you do not wish to modify
the collection c
and thus cannot call collection.retainAll(retain);
.
collection | the collection whose contents are the target of the #retailAll operation |
---|---|
retain | the collection containing the elements to be retained in the returned collection |
List
containing all the elements of c
that occur at least once in retain
.NullPointerException | if either parameter is null |
---|
Subtracts all elements in the second list from the first list, placing the results in a new list.
This differs from removeAll(Collection)
in that
cardinality is respected; if list1
contains two
occurrences of null
and list2
only
contains one occurrence, then the returned list will still contain
one occurrence.
list1 | the list to subtract from |
---|---|
list2 | the list to subtract |
NullPointerException | if either list is null |
---|
Returns the sum of the given lists. This is their intersection subtracted from their union.
list1 | the first list |
---|---|
list2 | the second list |
NullPointerException | if either list is null |
---|
Returns a synchronized list backed by the given list.
You must manually synchronize on the returned buffer's iterator to avoid non-deterministic behavior:
List list = ListUtils.synchronizedList(myList); synchronized (list) { Iterator i = list.iterator(); while (i.hasNext()) { process (i.next()); } }This method uses the implementation in the decorators subpackage.
list | the list to synchronize, must not be null |
---|
IllegalArgumentException | if the list is null |
---|
Returns a transformed list backed by the given list.
Each object is passed through the transformer as it is added to the List. It is important not to use the original list after invoking this method, as it is a backdoor for adding untransformed objects.
list | the list to predicate, must not be null |
---|---|
transformer | the transformer for the list, must not be null |
IllegalArgumentException | if the List or Transformer is null |
---|
Returns a typed list backed by the given list.
Only objects of the specified type can be added to the list.
list | the list to limit to a specific type, must not be null |
---|---|
type | the type of objects which may be added to the list |
Returns a new list containing the second list appended to the
first list. The addAll(Collection)
operation is
used to append the two given lists into a new list.
list1 | the first list |
---|---|
list2 | the second list |
NullPointerException | if either list is null |
---|
Returns an unmodifiable list backed by the given list.
This method uses the implementation in the decorators subpackage.
list | the list to make unmodifiable, must not be null |
---|
IllegalArgumentException | if the list is null |
---|