Class Lists

java.lang.Object
org.apache.hadoop.util.Lists

@Private public final class Lists extends Object
Static utility methods pertaining to List instances. This class is Hadoop's internal use alternative to Guava's Lists utility class. Javadocs for majority of APIs in this class are taken from Guava's Lists class from Guava release version 27.0-jre.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <E> ArrayList<E>
    Creates a mutable, empty ArrayList instance.
    static <E> ArrayList<E>
    newArrayList(E... elements)
    Creates a mutable ArrayList instance containing the given elements.
    static <E> ArrayList<E>
    newArrayList(Iterable<? extends E> elements)
    Creates a mutable ArrayList instance containing the given elements; a very thin shortcut for creating an empty list then calling Iterables#addAll.
    static <E> ArrayList<E>
    newArrayList(Iterator<? extends E> elements)
    Creates a mutable ArrayList instance containing the given elements; a very thin shortcut for creating an empty list and then calling Iterators#addAll.
    static <E> ArrayList<E>
    newArrayListWithCapacity(int initialArraySize)
    Creates an ArrayList instance backed by an array with the specified initial size; simply delegates to ArrayList(int).
    static <E> ArrayList<E>
    newArrayListWithExpectedSize(int estimatedSize)
    Creates an ArrayList instance to hold estimatedSize elements, plus an unspecified amount of padding; you almost certainly mean to call newArrayListWithCapacity(int) (see that method for further advice on usage).
    static <E> LinkedList<E>
    Creates a mutable, empty LinkedList instance.
    static <E> LinkedList<E>
    newLinkedList(Iterable<? extends E> elements)
    Creates a mutable LinkedList instance containing the given elements; a very thin shortcut for creating an empty list then calling Iterables#addAll.
    static <T> List<List<T>>
    partition(List<T> originalList, int pageSize)
    Returns consecutive sub-lists of a list, each of the same size (the final list may be smaller).

    Methods inherited from class java.lang.Object

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

    • newArrayList

      public static <E> ArrayList<E> newArrayList()
      Creates a mutable, empty ArrayList instance.
      Type Parameters:
      E - Generics Type E.
      Returns:
      ArrayList Generics Type E.
    • newArrayList

      @SafeVarargs public static <E> ArrayList<E> newArrayList(E... elements)
      Creates a mutable ArrayList instance containing the given elements.

      Note that even when you do need the ability to add or remove, this method provides only a tiny bit of syntactic sugar for newArrayList( asList (...)), or for creating an empty list then calling Collections.addAll(java.util.Collection<? super T>, T...).

      Type Parameters:
      E - Generics Type E.
      Parameters:
      elements - elements.
      Returns:
      ArrayList Generics Type E.
    • newArrayList

      public static <E> ArrayList<E> newArrayList(Iterable<? extends E> elements)
      Creates a mutable ArrayList instance containing the given elements; a very thin shortcut for creating an empty list then calling Iterables#addAll.
      Type Parameters:
      E - Generics Type E.
      Parameters:
      elements - elements.
      Returns:
      ArrayList Generics Type E.
    • newArrayList

      public static <E> ArrayList<E> newArrayList(Iterator<? extends E> elements)
      Creates a mutable ArrayList instance containing the given elements; a very thin shortcut for creating an empty list and then calling Iterators#addAll.
      Type Parameters:
      E - Generics Type E.
      Parameters:
      elements - elements.
      Returns:
      ArrayList Generics Type E.
    • newArrayListWithCapacity

      public static <E> ArrayList<E> newArrayListWithCapacity(int initialArraySize)
      Creates an ArrayList instance backed by an array with the specified initial size; simply delegates to ArrayList(int).
      Type Parameters:
      E - Generics Type E.
      Parameters:
      initialArraySize - the exact size of the initial backing array for the returned array list (ArrayList documentation calls this value the "capacity").
      Returns:
      a new, empty ArrayList which is guaranteed not to resize itself unless its size reaches initialArraySize + 1.
      Throws:
      IllegalArgumentException - if initialArraySize is negative.
    • newArrayListWithExpectedSize

      public static <E> ArrayList<E> newArrayListWithExpectedSize(int estimatedSize)
      Creates an ArrayList instance to hold estimatedSize elements, plus an unspecified amount of padding; you almost certainly mean to call newArrayListWithCapacity(int) (see that method for further advice on usage).
      Type Parameters:
      E - Generics Type E.
      Parameters:
      estimatedSize - an estimate of the eventual List.size() of the new list.
      Returns:
      a new, empty ArrayList, sized appropriately to hold the estimated number of elements.
      Throws:
      IllegalArgumentException - if estimatedSize is negative.
    • newLinkedList

      public static <E> LinkedList<E> newLinkedList()
      Creates a mutable, empty LinkedList instance.

      Performance note: ArrayList and ArrayDeque consistently outperform LinkedList except in certain rare and specific situations. Unless you have spent a lot of time benchmarking your specific needs, use one of those instead.

      Type Parameters:
      E - Generics Type E.
      Returns:
      Generics Type E List.
    • newLinkedList

      public static <E> LinkedList<E> newLinkedList(Iterable<? extends E> elements)
      Creates a mutable LinkedList instance containing the given elements; a very thin shortcut for creating an empty list then calling Iterables#addAll.

      Performance note: ArrayList and ArrayDeque consistently outperform LinkedList except in certain rare and specific situations. Unless you have spent a lot of time benchmarking your specific needs, use one of those instead.

      Type Parameters:
      E - Generics Type E.
      Parameters:
      elements - elements.
      Returns:
      Generics Type E List.
    • partition

      public static <T> List<List<T>> partition(List<T> originalList, int pageSize)
      Returns consecutive sub-lists of a list, each of the same size (the final list may be smaller).
      Type Parameters:
      T - Generics Type.
      Parameters:
      originalList - original big list.
      pageSize - desired size of each sublist ( last one may be smaller)
      Returns:
      a list of sub lists.