|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.commons.collections.ListUtils
public class ListUtils
Provides utility methods and decorators for List
instances.
Field Summary | |
---|---|
static List |
EMPTY_LIST
An empty unmodifiable list. |
Constructor Summary | |
---|---|
ListUtils()
ListUtils should not normally be instantiated. |
Method Summary | ||
---|---|---|
static
|
fixedSizeList(List<E> list)
Returns a fixed-sized list backed by the given list. |
|
static int |
hashCodeForList(Collection list)
Generates a hash code using the algorithm specified in List.hashCode() . |
|
static
|
intersection(List<? extends E> list1,
List<? extends E> list2)
Returns a new list containing all elements that are contained in both given lists. |
|
static
|
isEqualList(Collection<? extends E> list1,
Collection<? extends E> list2)
Tests two lists for value-equality as per the equality contract in List.equals(java.lang.Object) . |
|
static
|
lazyList(List<E> list,
Factory<? extends E> factory)
Returns a "lazy" list whose elements will be created on demand. |
|
static
|
predicatedList(List<E> list,
Predicate<? super E> predicate)
Returns a predicated (validating) list backed by the given list. |
|
static
|
subtract(List<? extends E> list1,
List<? extends E> list2)
Subtracts all elements in the second list from the first list, placing the results in a new list. |
|
static
|
sum(List<? extends E> list1,
List<? extends E> list2)
Returns the sum of the given lists. |
|
static
|
synchronizedList(List<E> list)
Returns a synchronized list backed by the given list. |
|
static
|
transformedList(List<I> list,
Transformer<? super I,? extends O> transformer)
Returns a transformed list backed by the given list. |
|
static
|
typedList(List<E> list,
Class<E> type)
Deprecated. Java generics makes this method obsolete. |
|
static
|
union(List<? extends E> list1,
List<? extends E> list2)
Returns a new list containing the second list appended to the first list. |
|
static
|
unmodifiableList(List<E> list)
Returns an unmodifiable list backed by the given list. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final List EMPTY_LIST
Collections
implementation
and is provided for completeness.
Constructor Detail |
---|
public ListUtils()
ListUtils
should not normally be instantiated.
Method Detail |
---|
public static <E> List<E> intersection(List<? extends E> list1, List<? extends E> list2)
list1
- the first listlist2
- the second list
NullPointerException
- if either list is nullpublic static <E> List<E> subtract(List<? extends E> list1, List<? extends E> list2)
List.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 fromlist2
- the list to subtract
NullPointerException
- if either list is nullpublic static <E> List<E> sum(List<? extends E> list1, List<? extends E> list2)
list1
- the first listlist2
- the second list
NullPointerException
- if either list is nullpublic static <E> List<E> union(List<? extends E> list1, List<? extends E> list2)
List.addAll(Collection)
operation is
used to append the two given lists into a new list.
list1
- the first listlist2
- the second list
NullPointerException
- if either list is nullpublic static <E> boolean isEqualList(Collection<? extends E> list1, Collection<? extends E> list2)
List.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 nulllist2
- the second list, may be null
List
public static int hashCodeForList(Collection list)
List.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
List.hashCode()
public static <E> List<E> synchronizedList(List<E> list)
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 nullpublic static <E> List<E> unmodifiableList(List<E> list)
list
- the list to make unmodifiable, must not be null
IllegalArgumentException
- if the list is nullpublic static <E> List<E> predicatedList(List<E> list, Predicate<? super E> predicate)
list
- the list to predicate, must not be nullpredicate
- the predicate for the list, must not be null
IllegalArgumentException
- if the List or Predicate is nullpublic static <E> List<E> typedList(List<E> list, Class<E> type)
list
- the list to limit to a specific type, must not be nulltype
- the type of objects which may be added to the list
public static <I,O> List<O> transformedList(List<I> list, Transformer<? super I,? extends O> transformer)
list
- the list to predicate, must not be nulltransformer
- the transformer for the list, must not be null
IllegalArgumentException
- if the List or Transformer is nullpublic static <E> List<E> lazyList(List<E> list, Factory<? extends E> factory)
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 nullfactory
- the factory for creating new objects, must not be null
IllegalArgumentException
- if the List or Factory is nullpublic static <E> List<E> fixedSizeList(List<E> list)
List.set(int,Object)
method).
list
- the list whose size to fix, must not be null
IllegalArgumentException
- if the List is null
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |