Class Lists

java.lang.Object
com.pnfsoftware.jeb.util.collect.Lists

public class Lists extends Object
Collection of utility functions to create of manipulate concrete or abstract lists.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> void
    add(List<T> list, int index, T elt)
    Add (insert) the element of a collection.
    static <T> void
    add(List<T> list, int index, T elt, T def)
    Add (insert) the element of a collection.
    static <T> List<T>
    add(List<T> dst, List<T> src)
     
    static <T> List<T>
    addNonNulls(List<T> dst, List<? extends T> src)
     
    static <T> List<T>
    addNonNulls(List<T> dst, T... elts)
     
    static <T extends Comparable<T>>
    int
    addSorted(List<T> sortedList, T elt)
    Insert an element into a sorted list at its natural position.
    static <T> ArrayList<T>
     
    static <T> ArrayList<T>
    createArrayList(Collection<? extends T>... colls)
     
    static <T> ArrayList<T>
     
    static <T> List<T>
    fromIterator(Iterable<T> iterable)
    Generate a list from an iterable.
    static <T> T
    get(List<? extends T> list, int index)
    Get the value of a list, safely returning null if the list is null or the index out-of-range.
    static <T> T
    get(List<? extends T> list, int index, T safeValue)
    Get the value of a list, safely returning a providing default value if the list s null or the index out-of-range.
    static <T> T
    getFirst(List<? extends T> list)
    Get the first value of a list, safely returning null if the list is null or the index out-of-range.
    static <T extends Comparable<T>>
    void
    insertIntoSortedList(List<T> list, T elt)
    Insert an additional element into a pre-sorted list and keep the list sorted.
    static <T> void
    insertIntoSortedList(List<T> list, T elt, Comparator<T> cmp, boolean reverse)
    Insert an additional element into a pre-sorted list and keep the list sorted.
    static <T> T
    last(List<T> list)
    Retrieve the last item of a List.
    static <T, R> List<? extends R>
    map(Collection<T> input, Function<? super T,? extends R> mapper)
    Apply a 1-to-1 map operation to the elements of a collection and return the list of resulting new elements.
    static <T> ArrayList<T>
    newArrayList(T... elts)
     
    static <T> List<T>
    reverse(List<T> list)
    Reverse the provided input list.
    static <T> ListIterator<T>
    Get a reverse iterator on the list.
    static <T> void
    rotate(List<T> list, boolean rotateForward)
    Rotate the elements of a list.
    static <T> void
    rotate(List<T> list, boolean rotateForward, int indexFirst, int indexLast)
    Rotate the elements of a list or a portion of the elements of a list.
    static <T> List<T>
    safe(List<T> list)
     
    static <T> T
    set(List<T> list, int index, T elt)
    Set the element of a collection.
    static <T> T
    set(List<T> list, int index, T elt, T def)
    Set the element of a collection.
    static <T> List<T>
    subList(List<T> list, int fromIndex)
    Get a tail sub-list of a list.

    Methods inherited from class java.lang.Object

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

    • Lists

      public Lists()
  • Method Details

    • addSorted

      public static <T extends Comparable<T>> int addSorted(List<T> sortedList, T elt)
      Insert an element into a sorted list at its natural position.
      Type Parameters:
      T -
      Parameters:
      sortedList - a sorted (natural sort) list of comparable elements
      elt - the element to be inserted
      Returns:
      the insertion index
    • createArrayList

      public static <T> ArrayList<T> createArrayList()
    • createArrayList

      public static <T> ArrayList<T> createArrayList(T elt)
    • createArrayList

      @SafeVarargs public static <T> ArrayList<T> createArrayList(Collection<? extends T>... colls)
    • newArrayList

      @SafeVarargs public static <T> ArrayList<T> newArrayList(T... elts)
    • add

      public static <T> List<T> add(List<T> dst, List<T> src)
    • addNonNulls

      public static <T> List<T> addNonNulls(List<T> dst, List<? extends T> src)
    • addNonNulls

      @SafeVarargs public static <T> List<T> addNonNulls(List<T> dst, T... elts)
    • subList

      public static <T> List<T> subList(List<T> list, int fromIndex)
      Get a tail sub-list of a list.
      Parameters:
      list -
      fromIndex -
      Returns:
    • get

      public static <T> T get(List<? extends T> list, int index, T safeValue)
      Get the value of a list, safely returning a providing default value if the list s null or the index out-of-range.
      Parameters:
      list -
      index -
      safeValue -
      Returns:
    • get

      public static <T> T get(List<? extends T> list, int index)
      Get the value of a list, safely returning null if the list is null or the index out-of-range.
      Parameters:
      list -
      index -
      Returns:
    • getFirst

      public static <T> T getFirst(List<? extends T> list)
      Get the first value of a list, safely returning null if the list is null or the index out-of-range.
      Parameters:
      list -
      Returns:
    • reverseIterator

      public static <T> ListIterator<T> reverseIterator(List<T> list)
      Get a reverse iterator on the list. The pointer is set to the last element of the list (if any).
      Parameters:
      list -
      Returns:
    • reverse

      public static <T> List<T> reverse(List<T> list)
      Reverse the provided input list.
      Parameters:
      list - a list
      Returns:
      the input list (same object), reversed
    • map

      public static <T, R> List<? extends R> map(Collection<T> input, Function<? super T,? extends R> mapper)
      Apply a 1-to-1 map operation to the elements of a collection and return the list of resulting new elements.
      Type Parameters:
      T - type of input element
      R - type of output element
      Parameters:
      input - input collection
      mapper - a map() function
      Returns:
      output list
    • set

      public static <T> T set(List<T> list, int index, T elt, T def)
      Set the element of a collection. If the index is negative, the method throws OOB. If it is greater or equal than the current number of elements in the list, additional elements are inserted using the provided default value.
      Type Parameters:
      T -
      Parameters:
      list -
      index -
      elt -
      def -
    • set

      public static <T> T set(List<T> list, int index, T elt)
      Set the element of a collection. If the index is negative, the method throws OOB. If it is greater or equal than the current number of elements in the list, additional elements are inserted using the null default value.
      Type Parameters:
      T -
      Parameters:
      list -
      index -
      elt -
      Returns:
    • add

      public static <T> void add(List<T> list, int index, T elt, T def)
      Add (insert) the element of a collection. If the index is negative, the method throws OOB. If it is greater that the current number of elements in the list, additional elements are inserted using the provided default value.
      Type Parameters:
      T -
      Parameters:
      list -
      index -
      elt -
      def -
    • add

      public static <T> void add(List<T> list, int index, T elt)
      Add (insert) the element of a collection. If the index is negative, the method throws OOB. If it is greater that the current number of elements in the list, additional elements are inserted using the null default value.
      Type Parameters:
      T -
      Parameters:
      list -
      index -
      elt -
    • safe

      public static <T> List<T> safe(List<T> list)
      Type Parameters:
      T -
      Parameters:
      list - input list, null
      Returns:
      the input list or an empty list if the input was null
    • fromIterator

      public static <T> List<T> fromIterator(Iterable<T> iterable)
      Generate a list from an iterable.
      Type Parameters:
      T -
      Parameters:
      iterable -
      Returns:
    • insertIntoSortedList

      public static <T extends Comparable<T>> void insertIntoSortedList(List<T> list, T elt)
      Insert an additional element into a pre-sorted list and keep the list sorted.
      Type Parameters:
      T -
      Parameters:
      list - a sorted list
      elt - the element to insert into the sorted list
    • insertIntoSortedList

      public static <T> void insertIntoSortedList(List<T> list, T elt, Comparator<T> cmp, boolean reverse)
      Insert an additional element into a pre-sorted list and keep the list sorted.
      Type Parameters:
      T -
      Parameters:
      list - a sorted list
      elt - the element to insert into the sorted list
      cmp - optional comparator to be used; if null, the elements must be comparable
      reverse - false if the list is sorted in natural (ascending) order, true if the list is sorted in reverse (descending) order
    • rotate

      public static <T> void rotate(List<T> list, boolean rotateForward)
      Rotate the elements of a list.
      Type Parameters:
      T -
      Parameters:
      list - a list
      rotateForward - if true, the elements at i are moved to i+1 (and the last element will replace the first one); else, the elements at i are moved to i-1 (and the first element will replace the last one)
    • rotate

      public static <T> void rotate(List<T> list, boolean rotateForward, int indexFirst, int indexLast)
      Rotate the elements of a list or a portion of the elements of a list.
      Type Parameters:
      T -
      Parameters:
      list - a list
      rotateForward - if true, the elements at i are moved to i+1 (and the last element will replace the first one); else, the elements at i are moved to i-1 (and the first element will replace the last one)
      indexFirst - first element to rotate (included)
      indexLast - last element to rotate (included), must be greater than indexFirst
    • last

      public static <T> T last(List<T> list)
      Retrieve the last item of a List.
      Type Parameters:
      T -
      Parameters:
      list - a list
      Returns:
      the last item at index list.size() - 1 or null if list is null or empty