public class ListUtils
extends java.lang.Object
List
instances.Modifier and Type | Method and Description |
---|---|
static <T> java.util.List<T> |
defaultIfNull(java.util.List<T> list,
java.util.List<T> defaultList)
Returns either the passed in list, or if the list is
null ,
the value of defaultList . |
static <T> java.util.List<T> |
emptyIfNull(java.util.List<T> list)
Returns an immutable empty list if the argument is
null ,
or the argument itself otherwise. |
static <E> java.util.List<E> |
fixedSizeList(java.util.List<E> list)
Returns a fixed-sized list backed by the given list.
|
static int |
hashCodeForList(java.util.Collection<?> list)
Generates a hash code using the algorithm specified in
List.hashCode() . |
static <E> int |
indexOf(java.util.List<E> list,
Predicate<E> predicate)
Finds the first index in the given List which matches the given predicate.
|
static <E> java.util.List<E> |
intersection(java.util.List<? extends E> list1,
java.util.List<? extends E> list2)
Returns a new list containing all elements that are contained in
both given lists.
|
static boolean |
isEqualList(java.util.Collection<?> list1,
java.util.Collection<?> list2)
Tests two lists for value-equality as per the equality contract in
List.equals(java.lang.Object) . |
static <E> java.util.List<E> |
lazyList(java.util.List<E> list,
Factory<? extends E> factory)
Returns a "lazy" list whose elements will be created on demand.
|
static java.lang.String |
longestCommonSubsequence(java.lang.CharSequence a,
java.lang.CharSequence b)
Returns the longest common subsequence (LCS) of two
CharSequence objects. |
static <E> java.util.List<E> |
longestCommonSubsequence(java.util.List<E> a,
java.util.List<E> b)
Returns the longest common subsequence (LCS) of two sequences (lists).
|
static <E> java.util.List<E> |
longestCommonSubsequence(java.util.List<E> a,
java.util.List<E> b,
Equator<? super E> equator)
Returns the longest common subsequence (LCS) of two sequences (lists).
|
static <T> java.util.List<java.util.List<T>> |
partition(java.util.List<T> list,
int size)
Returns consecutive
sublists of a
list, each of the same size (the final list may be smaller). |
static <E> java.util.List<E> |
predicatedList(java.util.List<E> list,
Predicate<E> predicate)
Returns a predicated (validating) list backed by the given list.
|
static <E> java.util.List<E> |
removeAll(java.util.Collection<E> collection,
java.util.Collection<?> remove)
Removes the elements in
remove from collection . |
static <E> java.util.List<E> |
retainAll(java.util.Collection<E> collection,
java.util.Collection<?> retain)
Returns a List containing all the elements in
collection
that are also in retain . |
static <E> java.util.List<E> |
select(java.util.Collection<? extends E> inputCollection,
Predicate<? super E> predicate)
Selects all elements from input collection which match the given
predicate into an output list.
|
static <E> java.util.List<E> |
selectRejected(java.util.Collection<? extends E> inputCollection,
Predicate<? super E> predicate)
Selects all elements from inputCollection which don't match the given
predicate into an output collection.
|
static <E> java.util.List<E> |
subtract(java.util.List<E> list1,
java.util.List<? extends E> list2)
Subtracts all elements in the second list from the first list,
placing the results in a new list.
|
static <E> java.util.List<E> |
sum(java.util.List<? extends E> list1,
java.util.List<? extends E> list2)
Returns the sum of the given lists.
|
static <E> java.util.List<E> |
synchronizedList(java.util.List<E> list)
Returns a synchronized list backed by the given list.
|
static <E> java.util.List<E> |
transformedList(java.util.List<E> list,
Transformer<? super E,? extends E> transformer)
Returns a transformed list backed by the given list.
|
static <E> java.util.List<E> |
union(java.util.List<? extends E> list1,
java.util.List<? extends E> list2)
Returns a new list containing the second list appended to the
first list.
|
static <E> java.util.List<E> |
unmodifiableList(java.util.List<? extends E> list)
Returns an unmodifiable list backed by the given list.
|
public static <T> java.util.List<T> emptyIfNull(java.util.List<T> list)
null
,
or the argument itself otherwise.T
- the element typelist
- the list, possibly null
null
public static <T> java.util.List<T> defaultIfNull(java.util.List<T> list, java.util.List<T> defaultList)
null
,
the value of defaultList
.T
- the element typelist
- the list, possibly null
defaultList
- the returned values if list is null
null
public static <E> java.util.List<E> intersection(java.util.List<? extends E> list1, java.util.List<? extends E> list2)
E
- the element typelist1
- the first listlist2
- the second listjava.lang.NullPointerException
- if either list is nullpublic static <E> java.util.List<E> subtract(java.util.List<E> list1, java.util.List<? extends E> list2)
This differs from 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.
E
- the element typelist1
- the list to subtract fromlist2
- the list to subtractjava.lang.NullPointerException
- if either list is nullpublic static <E> java.util.List<E> sum(java.util.List<? extends E> list1, java.util.List<? extends E> list2)
E
- the element typelist1
- the first listlist2
- the second listjava.lang.NullPointerException
- if either list is nullpublic static <E> java.util.List<E> union(java.util.List<? extends E> list1, java.util.List<? extends E> list2)
List.addAll(Collection)
operation is
used to append the two given lists into a new list.E
- the element typelist1
- the first listlist2
- the second listjava.lang.NullPointerException
- if either list is nullpublic static <E> java.util.List<E> select(java.util.Collection<? extends E> inputCollection, Predicate<? super E> predicate)
A null
predicate matches no elements.
E
- the element typeinputCollection
- the collection to get the input from, may not be nullpredicate
- the predicate to use, may be nulljava.lang.NullPointerException
- if the input list is nullCollectionUtils.select(Iterable, Predicate)
public static <E> java.util.List<E> selectRejected(java.util.Collection<? extends E> inputCollection, Predicate<? super E> predicate)
If the input predicate is null
, the result is an empty list.
E
- the element typeinputCollection
- the collection to get the input from, may not be nullpredicate
- the predicate to use, may be nulljava.lang.NullPointerException
- if the input collection is nullCollectionUtils.selectRejected(Iterable, Predicate)
public static boolean isEqualList(java.util.Collection<?> list1, java.util.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.true
if and only if both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elementse1
ande2
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 theList
interface.
list1
- the first list, may be nulllist2
- the second list, may be nullList
public static int hashCodeForList(java.util.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 nullList.hashCode()
public static <E> java.util.List<E> retainAll(java.util.Collection<E> collection, java.util.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.contains(Object)
.
E
- the element typecollection
- the collection whose contents are the target of the #retailAll operationretain
- the collection containing the elements to be retained in the returned collectionList
containing all the elements of c
that occur at least once in retain
.java.lang.NullPointerException
- if either parameter is nullpublic static <E> java.util.List<E> removeAll(java.util.Collection<E> collection, java.util.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.contains(Object)
.
E
- the element typecollection
- 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
.java.lang.NullPointerException
- if either parameter is nullpublic static <E> java.util.List<E> synchronizedList(java.util.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 typelist
- the list to synchronize, must not be nulljava.lang.NullPointerException
- if the list is nullpublic static <E> java.util.List<E> unmodifiableList(java.util.List<? extends E> list)
This method uses the implementation in the decorators subpackage.
E
- the element typelist
- the list to make unmodifiable, must not be nulljava.lang.NullPointerException
- if the list is nullpublic static <E> java.util.List<E> predicatedList(java.util.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 typelist
- the list to predicate, must not be nullpredicate
- the predicate for the list, must not be nulljava.lang.NullPointerException
- if the List or Predicate is nullpublic static <E> java.util.List<E> transformedList(java.util.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.transformedList(java.util.List<E>, org.apache.commons.collections4.Transformer<? super E, ? extends E>)
.
E
- the element typelist
- the list to predicate, must not be nulltransformer
- the transformer for the list, must not be nulljava.lang.NullPointerException
- if the List or Transformer is nullpublic static <E> java.util.List<E> lazyList(java.util.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 typelist
- the list to make lazy, must not be nullfactory
- the factory for creating new objects, must not be nulljava.lang.NullPointerException
- if the List or Factory is nullpublic static <E> java.util.List<E> fixedSizeList(java.util.List<E> list)
List.set(int, Object)
method).E
- the element typelist
- the list whose size to fix, must not be nulljava.lang.NullPointerException
- if the List is nullpublic static <E> int indexOf(java.util.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 typelist
- the List to search, may be nullpredicate
- the predicate to use, may be nullpublic static <E> java.util.List<E> longestCommonSubsequence(java.util.List<E> a, java.util.List<E> b)
E
- the element typea
- the first listb
- the second listjava.lang.NullPointerException
- if either list is null
public static <E> java.util.List<E> longestCommonSubsequence(java.util.List<E> a, java.util.List<E> b, Equator<? super E> equator)
E
- the element typea
- the first listb
- the second listequator
- the equator used to test object equalityjava.lang.NullPointerException
- if either list or the equator is null
public static java.lang.String longestCommonSubsequence(java.lang.CharSequence a, java.lang.CharSequence b)
CharSequence
objects.
This is a convenience method for using longestCommonSubsequence(List, List)
with CharSequence
instances.
a
- the first sequenceb
- the second sequenceString
java.lang.NullPointerException
- if either sequence is null
public static <T> java.util.List<java.util.List<T>> partition(java.util.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.subList(int, int)
, 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 typelist
- the list to return consecutive sublists ofsize
- the desired size of each sublist (the last may be smaller)java.lang.NullPointerException
- if list is nulljava.lang.IllegalArgumentException
- if size is not strictly positive"Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"