Package snap.util

Class ListUtils

java.lang.Object
snap.util.ListUtils

public class ListUtils extends Object
Utility methods for use with Java.util.List.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> List<T>
    add(List<T> aList, T anObj)
    Adds an object to the given list and returns list (creates list if missing).
    static <T> void
    addAllUnique(List<T> aList, int anIndex, T... theObjects)
    Adds all object from second list to first list (creates first list if missing).
    static <T> void
    addAllUnique(List<T> aList, T... theObjects)
    Adds all object from second list to first list (creates first list if missing).
    static <T> void
    addUnique(List<T> aList, T anObj)
    Adds an object to the given list if object is absent (creates list if missing).
    static <T> void
    addUniqueId(List<T> aList, T anObj)
    Adds an object to the given list if identical object is missing (creates list if missing).
    static <T> List<T>
    clone(List<T> aList)
    Returns a copy of the given list.
    static boolean
    contains(List<?> aList, Object anObj)
    Returns whether list contains given object (accepts null list).
    static boolean
    containsId(List<?> aList, Object anObj)
    Returns whether list contains identical given object (accepts null list).
    static boolean
    equalsId(List<?> aList1, List<?> aList2)
    Returns whether lists have same objects in them.
    static <T> List<T>
    filter(Collection<T> aList, Predicate<? super T> pred)
    Returns a filtered array for given original and Predicate.
    static <T, R> R[]
    filterToArray(Collection<T> aList, Predicate<? super T> pred, Class<R> aClass)
    Returns a filtered array for given original and Predicate.
    static <T> T
    findMatch(Collection<T> aList, Predicate<? super T> aPred)
    Returns the first item in collection that matches given predicate (or null).
    static <T> int
    findMatchIndex(List<T> aList, Predicate<? super T> aPred)
    Returns the index of first item in list that matches given predicate (or -1).
    static <T> T
    get(List<T> aList, int anIndex)
    Returns the object at the given index (returns null object for null list or invalid index).
    static <T> List<T>
    getFiltered(Collection<T> aList, Predicate<? super T> pred)
    Returns a list of derived items for given collection of original items.
    static <T> T
    getLast(List<T> aList)
    Returns the last object in the given list.
    static <T> boolean
    hasMatch(Collection<T> aList, Predicate<? super T> aPred)
    Returns whether given collection has match for given predicate.
    static int
    indexOfId(List<?> aList, Object anObj)
    Returns index of identical given object in given list.
    static <T> String
    joinString(List<T> aList, String aDelim, Function<T,String> aFunc)
    Joins a list of items by given delimiter using given function to get item strings.
    static String
    joinStrings(List<?> aList, String aString)
    Returns a string by concatenating strings in given list separated by given string.
    static <T, R> List<R>
    map(Collection<T> aList, Function<? super T,? extends R> aFunction)
    Returns a mapped array for given original and Function.
    static <T, R> R[]
    mapToArray(Collection<T> aList, Function<? super T,? extends R> aFunction, Class<R> aClass)
    Returns a mapped array for given original and Function.
    static <T> void
    moveToFront(List<T> aList, int anIndex)
    Moves the object at the given index to the front of the list.
    static <T> void
    moveToFront(List<T> aList, T anObj)
    Move the given object to the front of the list.
    static int
    removeId(List<?> aList, Object anObj)
    Removes the object identical to the given object from list.
    static <T> T
    removeLast(List<T> aList)
    Removes the last object from given list.
    static <T> List<T>
    reverse(List<T> aList)
    Reverses the items in the given list.
    static int
    size(List<?> aList)
    Returns the size of a list (accepts null list).
    static <T> void
    xor(List<T> list1, List<T> list2)
    Adds object from list 2 to list 1, unless they are already present (then removes them).

    Methods inherited from class java.lang.Object

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

    • ListUtils

      public ListUtils()
  • Method Details

    • size

      public static int size(List<?> aList)
      Returns the size of a list (accepts null list).
    • get

      public static <T> T get(List<T> aList, int anIndex)
      Returns the object at the given index (returns null object for null list or invalid index).
    • getLast

      public static <T> T getLast(List<T> aList)
      Returns the last object in the given list.
    • contains

      public static boolean contains(List<?> aList, Object anObj)
      Returns whether list contains given object (accepts null list).
    • containsId

      public static boolean containsId(List<?> aList, Object anObj)
      Returns whether list contains identical given object (accepts null list).
    • indexOfId

      public static int indexOfId(List<?> aList, Object anObj)
      Returns index of identical given object in given list.
    • add

      public static <T> List<T> add(List<T> aList, T anObj)
      Adds an object to the given list and returns list (creates list if missing).
    • addUnique

      public static <T> void addUnique(List<T> aList, T anObj)
      Adds an object to the given list if object is absent (creates list if missing).
    • addUniqueId

      public static <T> void addUniqueId(List<T> aList, T anObj)
      Adds an object to the given list if identical object is missing (creates list if missing).
    • addAllUnique

      public static <T> void addAllUnique(List<T> aList, T... theObjects)
      Adds all object from second list to first list (creates first list if missing).
    • addAllUnique

      public static <T> void addAllUnique(List<T> aList, int anIndex, T... theObjects)
      Adds all object from second list to first list (creates first list if missing).
    • removeLast

      public static <T> T removeLast(List<T> aList)
      Removes the last object from given list.
    • removeId

      public static int removeId(List<?> aList, Object anObj)
      Removes the object identical to the given object from list.
    • reverse

      public static <T> List<T> reverse(List<T> aList)
      Reverses the items in the given list.
    • joinStrings

      public static String joinStrings(List<?> aList, String aString)
      Returns a string by concatenating strings in given list separated by given string.
    • xor

      public static <T> void xor(List<T> list1, List<T> list2)
      Adds object from list 2 to list 1, unless they are already present (then removes them).
    • moveToFront

      public static <T> void moveToFront(List<T> aList, int anIndex)
      Moves the object at the given index to the front of the list.
    • moveToFront

      public static <T> void moveToFront(List<T> aList, T anObj)
      Move the given object to the front of the list.
    • equalsId

      public static boolean equalsId(List<?> aList1, List<?> aList2)
      Returns whether lists have same objects in them.
    • clone

      public static <T> List<T> clone(List<T> aList)
      Returns a copy of the given list.
    • getFiltered

      public static <T> List<T> getFiltered(Collection<T> aList, Predicate<? super T> pred)
      Returns a list of derived items for given collection of original items.
    • filter

      public static <T> List<T> filter(Collection<T> aList, Predicate<? super T> pred)
      Returns a filtered array for given original and Predicate.
    • filterToArray

      public static <T, R> R[] filterToArray(Collection<T> aList, Predicate<? super T> pred, Class<R> aClass)
      Returns a filtered array for given original and Predicate.
    • map

      public static <T, R> List<R> map(Collection<T> aList, Function<? super T,? extends R> aFunction)
      Returns a mapped array for given original and Function.
    • mapToArray

      public static <T, R> R[] mapToArray(Collection<T> aList, Function<? super T,? extends R> aFunction, Class<R> aClass)
      Returns a mapped array for given original and Function.
    • hasMatch

      public static <T> boolean hasMatch(Collection<T> aList, Predicate<? super T> aPred)
      Returns whether given collection has match for given predicate.
    • findMatch

      public static <T> T findMatch(Collection<T> aList, Predicate<? super T> aPred)
      Returns the first item in collection that matches given predicate (or null).
    • findMatchIndex

      public static <T> int findMatchIndex(List<T> aList, Predicate<? super T> aPred)
      Returns the index of first item in list that matches given predicate (or -1).
    • joinString

      public static <T> String joinString(List<T> aList, String aDelim, Function<T,String> aFunc)
      Joins a list of items by given delimiter using given function to get item strings.