| Modifier and Type | Method and Description |
|---|---|
static <T> List |
defaultIfNull(List
Returns either the passed in list, or if the list is
null, the value of
defaultList.
|
static <T> List |
emptyIfNull(List
Returns an immutable empty list if the argument is
null, or the argument itself otherwise.
|
static <E> List |
fixedSizeList(List
Returns a fixed-sized list backed by the given list.
|
static int |
hashCodeForList(Collection
Generates a hash code using the algorithm specified in
List.
|
static <E> int |
indexOf(List
Finds the first index in the given List which matches the given predicate.
|
static <E> List |
intersection(List
Returns a new list containing all elements that are contained in both given lists.
|
static boolean |
isEqualList(Collection
Tests two lists for value-equality as per the equality contract in
List.
|
static <E> List |
lazyList(List
Returns a "lazy" list whose elements will be created on demand.
|
static String |
longestCommonSubsequence(CharSequence
Returns the longest common subsequence (LCS) of two
CharSequence objects.
|
static <E> List |
longestCommonSubsequence(List
Returns the longest common subsequence (LCS) of two sequences (lists).
|
static <E> List |
longestCommonSubsequence(List
Returns the longest common subsequence (LCS) of two sequences (lists).
|
static <T> List |
partition(List
Returns consecutive
sublists of a list, each of the same size (the final list may be smaller).
|
static <E> List |
predicatedList(List
Returns a predicated (validating) list backed by the given list.
|
static <E> List |
removeAll(Collection
Removes the elements in
remove from
collection.
|
static <E> List |
retainAll(Collection
Returns a List containing all the elements in
collection that are also in
retain.
|
static <E> List |
select(Collection
Selects all elements from input collection which match the given predicate into an output list.
|
static <E> List |
selectRejected(Collection
Selects all elements from inputCollection which don't match the given predicate into an output collection.
|
static <E> List |
subtract(List
Subtracts all elements in the second list from the first list, placing the results in a new list.
|
static <E> List |
sum(List
Returns the sum of the given lists.
|
static <E> List |
synchronizedList(List
Returns a synchronized list backed by the given list.
|
static <E> List |
transformedList(List
Returns a transformed list backed by the given list.
|
static <E> List |
union(List
Returns a new list containing the second list appended to the first list.
|
static <E> List |
unmodifiableList(List
Returns an unmodifiable list backed by the given list.
|
public static <T> List<T> emptyIfNull(List <T> list)
null, or the argument itself otherwise.
T - the element type
list - the list, possibly
null
null
public static <T> List<T> defaultIfNull(List <T> list, List <T> defaultList)
null, the value of
defaultList.
T - the element type
list - the list, possibly
null
defaultList - the returned values if list is
null
null
public static <E> List<E> intersection(List <? extends E> list1, List <? extends E> list2)
E - the element type
list1 - the first list
list2 - the second list
NullPointerException - if either list is null
public static <E> List<E> subtract(List <E> list1, List <? extends E> list2)
This differs from List 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.
E - the element type
list1 - the list to subtract from
list2 - the list to subtract
NullPointerException - if either list is null
public static <E> List<E> sum(List <? extends E> list1, List <? extends E> list2)
E - the element type
list1 - the first list
list2 - the second list
NullPointerException - if either list is null
public 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.
E - the element type
list1 - the first list
list2 - the second list
NullPointerException - if either list is null
public static <E> List<E> select(Collection <? extends E> inputCollection, Predicate <? super E> predicate)
A null predicate matches no elements.
E - the element type
inputCollection - the collection to get the input from, may not be null
predicate - the predicate to use, may be null
NullPointerException - if the input list is null
CollectionUtils.select(Iterable, Predicate)
public static <E> List<E> selectRejected(Collection <? extends E> inputCollection, Predicate <? super E> predicate)
If the input predicate is null, the result is an empty list.
E - the element type
inputCollection - the collection to get the input from, may not be null
predicate - the predicate to use, may be null
NullPointerException - if the input collection is null
CollectionUtils.selectRejected(Iterable, Predicate)
public static boolean isEqualList(Collection<?> list1, Collection <?> 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. ReturnsNote: The behaviour of this method is undefined if the lists are modified during the equals comparison.trueif and only if both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elementse1ande2are 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 theListinterface.
list1 - the first list, may be null
list2 - 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> retainAll(Collection <E> collection, Collection <?> retain)
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);.
This implementation iterates over collection, checking each element in turn to see if it's contained in retain. If it's contained, it's added to the returned list. As a consequence, it is advised to use a collection type for retain that provides a fast (e.g. O(1)) implementation of Collection.
E - the element type
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
public static <E> List<E> removeAll(Collection <E> collection, Collection <?> remove)
remove from
collection. That is, this method returns a list containing all the elements in
collection 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);.
This implementation iterates over collection, checking each element in turn to see if it's contained in remove. If it's not contained, it's added to the returned list. As a consequence, it is advised to use a collection type for remove that provides a fast (e.g. O(1)) implementation of Collection.
E - the element type
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
public static <E> List<E> synchronizedList(List <E> list)
You must manually synchronize on the returned list'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 is just a wrapper for
Collections.synchronizedList(List) .
E - the element type
list - the list to synchronize, must not be null
NullPointerException - if the list is null
public static <E> List<E> unmodifiableList(List <? extends E> list)
This method uses the implementation in the decorators subpackage.
E - the element type
list - the list to make unmodifiable, must not be null
NullPointerException - if the list is null
public static <E> List<E> predicatedList(List <E> list, Predicate <E> predicate)
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.
E - the element type
list - the list to predicate, must not be null
predicate - the predicate for the list, must not be null
NullPointerException - if the List or Predicate is null
public static <E> List<E> transformedList(List <E> list, Transformer <? super E ,? extends E> transformer)
This method returns a new list (decorating the specified list) that will transform any new entries added to it. Existing entries in the specified list will not be transformed.
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.
Existing entries in the specified list will not be transformed. If you want that behaviour, see TransformedList.
E - the element type
list - the list to predicate, must not be null
transformer - the transformer for the list, must not be null
NullPointerException - if the List or Transformer is null
public static <E> List<E> lazyList(List <E> list, Factory <? extends E> factory)
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<Date> factory = new Factory<Date>() {
public Date create() {
return new Date();
}
}
List<Date> lazy = ListUtils.lazyList(new ArrayList<Date>(), factory);
Date date = lazy.get(3);
After the above code is executed,
date will refer to 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.
E - the element type
list - the list to make lazy, must not be null
factory - the factory for creating new objects, must not be null
NullPointerException - if the List or Factory is null
public static <E> List<E> fixedSizeList(List <E> list)
List.set(int, Object) method).
E - the element type
list - the list whose size to fix, must not be null
NullPointerException - if the List is null
public static <E> int indexOf(List<E> list, Predicate <E> predicate)
If the input List or predicate is null, or no element of the List matches the predicate, -1 is returned.
E - the element type
list - the List to search, may be null
predicate - the predicate to use, may be null
public static <E> List<E> longestCommonSubsequence(List <E> a, List <E> b)
E - the element type
a - the first list
b - the second list
NullPointerException - if either list is
null
public static <E> List<E> longestCommonSubsequence(List <E> a, List <E> b, Equator <? super E> equator)
E - the element type
a - the first list
b - the second list
equator - the equator used to test object equality
NullPointerException - if either list or the equator is
null
public static StringlongestCommonSubsequence(CharSequence a, CharSequence b)
CharSequence objects.
This is a convenience method for using longestCommonSubsequence(List, List) with CharSequence instances.
a - the first sequence
b - the second sequence
String
NullPointerException - if either sequence is
null
public static <T> List<List <T>> partition(List <T> list, int size)
sublists of a list, each of the same size (the final list may be smaller). For example, partitioning a list containing
[a, b, c, d, e] with a partition size of 3 yields
[[a, b, c], [d, e]] -- an outer list containing two inner lists of three and two elements, all in the original order.
The outer list is unmodifiable, but reflects the latest state of the source list. The inner lists are sublist views of the original list, produced on demand using List, and are subject to all the usual caveats about modification as explained in that API.
Adapted from http://code.google.com/p/guava-libraries/
T - the element type
list - the list to return consecutive sublists of
size - the desired size of each sublist (the last may be smaller)
NullPointerException - if list is null
IllegalArgumentException - if size is not strictly positive