Class ListUtils


  • public class ListUtils
    extends java.lang.Object
    Provides utility methods and decorators for List instances.
    Since:
    1.0
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method 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 <E> java.util.List<E> lazyList​(java.util.List<E> list, Transformer<java.lang.Integer,​? extends E> transformer)
      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.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • emptyIfNull

        public 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.
        Type Parameters:
        T - the element type
        Parameters:
        list - the list, possibly null
        Returns:
        an empty list if the argument is null
      • defaultIfNull

        public 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.
        Type Parameters:
        T - the element type
        Parameters:
        list - the list, possibly null
        defaultList - the returned values if list is null
        Returns:
        an empty list if the argument is null
        Since:
        4.0
      • intersection

        public 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.
        Type Parameters:
        E - the element type
        Parameters:
        list1 - the first list
        list2 - the second list
        Returns:
        the intersection of those two lists
        Throws:
        java.lang.NullPointerException - if either list is null
      • subtract

        public 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.

        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.

        Type Parameters:
        E - the element type
        Parameters:
        list1 - the list to subtract from
        list2 - the list to subtract
        Returns:
        a new list containing the results
        Throws:
        java.lang.NullPointerException - if either list is null
      • sum

        public 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. This is their intersection subtracted from their union.
        Type Parameters:
        E - the element type
        Parameters:
        list1 - the first list
        list2 - the second list
        Returns:
        a new list containing the sum of those lists
        Throws:
        java.lang.NullPointerException - if either list is null
      • union

        public 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. The List.addAll(Collection) operation is used to append the two given lists into a new list.
        Type Parameters:
        E - the element type
        Parameters:
        list1 - the first list
        list2 - the second list
        Returns:
        a new list containing the union of those lists
        Throws:
        java.lang.NullPointerException - if either list is null
      • select

        public 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.

        A null predicate matches no elements.

        Type Parameters:
        E - the element type
        Parameters:
        inputCollection - the collection to get the input from, may not be null
        predicate - the predicate to use, may be null
        Returns:
        the elements matching the predicate (new list)
        Throws:
        java.lang.NullPointerException - if the input list is null
        Since:
        4.0
        See Also:
        CollectionUtils.select(Iterable, Predicate)
      • selectRejected

        public 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.

        If the input predicate is null, the result is an empty list.

        Type Parameters:
        E - the element type
        Parameters:
        inputCollection - the collection to get the input from, may not be null
        predicate - the predicate to use, may be null
        Returns:
        the elements not matching the predicate (new list)
        Throws:
        java.lang.NullPointerException - if the input collection is null
        Since:
        4.0
        See Also:
        CollectionUtils.selectRejected(Iterable, Predicate)
      • isEqualList

        public 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).

        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.
        Parameters:
        list1 - the first list, may be null
        list2 - the second list, may be null
        Returns:
        whether the lists are equal by value comparison
        See Also:
        List
      • hashCodeForList

        public static int hashCodeForList​(java.util.Collection<?> list)
        Generates a hash code using the algorithm specified in 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.

        Parameters:
        list - the list to generate the hashCode for, may be null
        Returns:
        the hash code
        See Also:
        List.hashCode()
      • retainAll

        public 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. 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).

        Type Parameters:
        E - the element type
        Parameters:
        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
        Returns:
        a List containing all the elements of c that occur at least once in retain.
        Throws:
        java.lang.NullPointerException - if either parameter is null
        Since:
        3.2
      • removeAll

        public static <E> java.util.List<E> removeAll​(java.util.Collection<E> collection,
                                                      java.util.Collection<?> remove)
        Removes the elements in 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).

        Type Parameters:
        E - the element type
        Parameters:
        collection - the collection from which items are removed (in the returned collection)
        remove - the items to be removed from the returned collection
        Returns:
        a List containing all the elements of c except any elements that also occur in remove.
        Throws:
        java.lang.NullPointerException - if either parameter is null
        Since:
        3.2
      • synchronizedList

        public static <E> java.util.List<E> synchronizedList​(java.util.List<E> list)
        Returns a synchronized list backed by the given 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).
        Type Parameters:
        E - the element type
        Parameters:
        list - the list to synchronize, must not be null
        Returns:
        a synchronized list backed by the given list
        Throws:
        java.lang.NullPointerException - if the list is null
      • unmodifiableList

        public static <E> java.util.List<E> unmodifiableList​(java.util.List<? extends E> list)
        Returns an unmodifiable list backed by the given list.

        This method uses the implementation in the decorators subpackage.

        Type Parameters:
        E - the element type
        Parameters:
        list - the list to make unmodifiable, must not be null
        Returns:
        an unmodifiable list backed by the given list
        Throws:
        java.lang.NullPointerException - if the list is null
      • predicatedList

        public 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.

        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.

        Type Parameters:
        E - the element type
        Parameters:
        list - the list to predicate, must not be null
        predicate - the predicate for the list, must not be null
        Returns:
        a predicated list backed by the given list
        Throws:
        java.lang.NullPointerException - if the List or Predicate is null
      • transformedList

        public 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.

        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>).

        Type Parameters:
        E - the element type
        Parameters:
        list - the list to predicate, must not be null
        transformer - the transformer for the list, must not be null
        Returns:
        a transformed list backed by the given list
        Throws:
        java.lang.NullPointerException - if the List or Transformer is null
      • lazyList

        public 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.

        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.
        Type Parameters:
        E - the element type
        Parameters:
        list - the list to make lazy, must not be null
        factory - the factory for creating new objects, must not be null
        Returns:
        a lazy list backed by the given list
        Throws:
        java.lang.NullPointerException - if the List or Factory is null
      • lazyList

        public static <E> java.util.List<E> lazyList​(java.util.List<E> list,
                                                     Transformer<java.lang.Integer,​? extends E> transformer)
        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 transformer will be used to create a new object and that object will be inserted at that index.

        For instance:

         List<Integer> hours = Arrays.asList(7, 5, 8, 2);
         Transformer<Integer,Date> transformer = input -> LocalDateTime.now().withHour(hours.get(input));
         List<LocalDateTime> lazy = ListUtils.lazyList(new ArrayList<LocalDateTime>(), transformer);
         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.
        Type Parameters:
        E - the element type
        Parameters:
        list - the list to make lazy, must not be null
        transformer - the transformer for creating new objects, must not be null
        Returns:
        a lazy list backed by the given list
        Throws:
        java.lang.NullPointerException - if the List or Transformer is null
      • fixedSizeList

        public static <E> java.util.List<E> fixedSizeList​(java.util.List<E> list)
        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 List.set(int, Object) method).
        Type Parameters:
        E - the element type
        Parameters:
        list - the list whose size to fix, must not be null
        Returns:
        a fixed-size list backed by that list
        Throws:
        java.lang.NullPointerException - if the List is null
      • indexOf

        public 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.

        If the input List or predicate is null, or no element of the List matches the predicate, -1 is returned.

        Type Parameters:
        E - the element type
        Parameters:
        list - the List to search, may be null
        predicate - the predicate to use, may be null
        Returns:
        the first index of an Object in the List which matches the predicate or -1 if none could be found
      • longestCommonSubsequence

        public 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).
        Type Parameters:
        E - the element type
        Parameters:
        a - the first list
        b - the second list
        Returns:
        the longest common subsequence
        Throws:
        java.lang.NullPointerException - if either list is null
        Since:
        4.0
      • longestCommonSubsequence

        public 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).
        Type Parameters:
        E - the element type
        Parameters:
        a - the first list
        b - the second list
        equator - the equator used to test object equality
        Returns:
        the longest common subsequence
        Throws:
        java.lang.NullPointerException - if either list or the equator is null
        Since:
        4.0
      • longestCommonSubsequence

        public static java.lang.String longestCommonSubsequence​(java.lang.CharSequence a,
                                                                java.lang.CharSequence b)
        Returns the longest common subsequence (LCS) of two CharSequence objects.

        This is a convenience method for using longestCommonSubsequence(List, List) with CharSequence instances.

        Parameters:
        a - the first sequence
        b - the second sequence
        Returns:
        the longest common subsequence as String
        Throws:
        java.lang.NullPointerException - if either sequence is null
        Since:
        4.0
      • partition

        public 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). 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/

        Type Parameters:
        T - the element type
        Parameters:
        list - the list to return consecutive sublists of
        size - the desired size of each sublist (the last may be smaller)
        Returns:
        a list of consecutive sublists
        Throws:
        java.lang.NullPointerException - if list is null
        java.lang.IllegalArgumentException - if size is not strictly positive
        Since:
        4.0