Class ListUtil

java.lang.Object
com.onenetwork.platform.tools.collections.ListUtil

public final class ListUtil
extends java.lang.Object
Utilities for List manipulation.
  • Method Summary

    Modifier and Type Method Description
    static <T> java.util.List<T> combine​(java.util.Collection<java.util.List<T>> collectionList)
    Combines a collection of Lists into one List by using List.addAll(Collection).
    static <T> java.util.List<T> combine​(java.util.List<T>... lists)
    Combines an arbitrary number of Lists into one List by using List.addAll(Collection).
    static <T> java.util.List<T> create​(T element)
    Convenience method for creating a new, mutable List containing a single element.
    static <T> java.util.List<T> create​(T... elements)
    Convenience method for creating a new, mutable List given homogeneous varargs.
    static <T> java.util.List<T> fill​(java.util.List<T> list, T val, int desiredLength)
    Given a list, appends "val" to the end of the List until it reaches desiredLength.
    static <T> T firstOrNull​(java.util.List<? extends T> list)
    Returns the first entry in the List, or null if the List is empty.
    static <T> T last​(java.util.List<? extends T> list)
    Returns the last entry in the list, or throws IllegalArgumentException if the list is null or empty.
    static <U,​ T extends U>
    java.util.List<T>
    select​(java.util.List<? extends U> source, java.lang.Class<T> c)
    Returns a typesafe List of all Objects in source which are of Class c or a subclass of c.
    static <T extends java.lang.Comparable<T>>
    java.util.List<T>
    sorted​(java.util.List<T> list)
    Returns a new copy of the given list which has been sorted by its natural order.
    static <M> java.util.List<java.util.List<M>> split​(java.util.List<M> list, int maxRecords)
    Like subLists(List, int), but returns a typesafe List of Lists instead of a List array.
    static java.util.List[] subLists​(java.util.List list, int maxRecords)
    "Splits" the supplied List into multiple sub-Lists, where each List has a max of maxRecords entries.
    static <T> void swap​(int index1, int index2, java.util.List<T> list)
    Swap two values by index.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • create

      public static <T> java.util.List<T> create​(T element)
      Convenience method for creating a new, mutable List containing a single element. The list returned is mutable.
      Type Parameters:
      T - element type
      Parameters:
      element - element to go into the list
      Returns:
      new List containing the given element
    • create

      @SafeVarargs public static <T> java.util.List<T> create​(T... elements)
      Convenience method for creating a new, mutable List given homogeneous varargs. The list returned is mutable, unlike Arrays#asList().

      Sample usage: List<String> strings = ListUtil.newList("A", "B", "C");

      Type Parameters:
      T - element type
      Parameters:
      elements - elements to go into the list
      Returns:
      new List containing all given elements
    • combine

      @SafeVarargs public static <T> java.util.List<T> combine​(java.util.List<T>... lists)
      Combines an arbitrary number of Lists into one List by using List.addAll(Collection).
      Type Parameters:
      T - element type
      Parameters:
      lists - Lists which will be combined into one List
      Returns:
      new List which is a superset of all given Lists
    • combine

      public static <T> java.util.List<T> combine​(java.util.Collection<java.util.List<T>> collectionList)
      Combines a collection of Lists into one List by using List.addAll(Collection).
      Type Parameters:
      T - element type
      Parameters:
      lists - Lists which will be combined into one List
      Returns:
      new List which is a superset of all given Lists
    • split

      public static <M> java.util.List<java.util.List<M>> split​(java.util.List<M> list, int maxRecords)
      Like subLists(List, int), but returns a typesafe List of Lists instead of a List array.
    • subLists

      public static java.util.List[] subLists​(java.util.List list, int maxRecords)
      "Splits" the supplied List into multiple sub-Lists, where each List has a max of maxRecords entries.
      Parameters:
      list - List of records
      maxRecords - max number of records in any sublist
      Returns:
      [0..len-2] will contain a List of size maxRecords, [len-1] will contain the rest of the entries
    • select

      public static <U,​ T extends U> java.util.List<T> select​(java.util.List<? extends U> source, java.lang.Class<T> c)
      Returns a typesafe List of all Objects in source which are of Class c or a subclass of c.
      Parameters:
      source - List of Objects
      c - Class to check against the Object instances in the LIst
    • last

      public static <T> T last​(java.util.List<? extends T> list)
      Returns the last entry in the list, or throws IllegalArgumentException if the list is null or empty.
      Type Parameters:
      T - type
      Parameters:
      list - from which to get last value
      Returns:
      last value of list
    • firstOrNull

      public static <T> T firstOrNull​(java.util.List<? extends T> list)
      Returns the first entry in the List, or null if the List is empty.
      Type Parameters:
      T - type
      Parameters:
      list - list to fetch from
      Returns:
      first value in list if size > 0, else null
    • swap

      public static <T> void swap​(int index1, int index2, java.util.List<T> list)
      Swap two values by index.
      Parameters:
      index1 - first value's index
      index2 - second value's index
      list - List whose values should be swapped
    • fill

      public static <T> java.util.List<T> fill​(java.util.List<T> list, T val, int desiredLength)
      Given a list, appends "val" to the end of the List until it reaches desiredLength. For example:
       ListUtil.fill(ListUtil.create(1, 2, 3), 0, 5)
       
      produces
       [1, 2, 3, 0, 0]
       
    • sorted

      public static <T extends java.lang.Comparable<T>> java.util.List<T> sorted​(java.util.List<T> list)
      Returns a new copy of the given list which has been sorted by its natural order.