Class ListUtils


  • public final class ListUtils
    extends java.lang.Object
    Immutable List utilities to polyfill Java 9+ features.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.util.List<T> copyOf​(java.util.Collection<? extends T> values)
      Creates an immutable copy of the given list.
      static <T> java.util.List<T> of()
      Returns an unmodifiable list containing zero entries.
      static <T> java.util.List<T> of​(T value)
      Returns an unmodifiable list containing a single entry.
      static <T> java.util.List<T> of​(T... values)
      Returns an unmodifiable list containing any number of entries.
      static <T> java.util.List<T> of​(T value1, T value2)
      Returns an unmodifiable list containing two entries.
      static <T> java.util.stream.Collector<T,​?,​java.util.List<T>> toUnmodifiableList()
      Creates a collector that collects into an unmodifiable list.
      • Methods inherited from class java.lang.Object

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

      • copyOf

        public static <T> java.util.List<T> copyOf​(java.util.Collection<? extends T> values)
        Creates an immutable copy of the given list.
        Type Parameters:
        T - the List's value type.
        Parameters:
        values - The collection to make an immutable list of.
        Returns:
        An immutable List copy.
      • of

        public static <T> java.util.List<T> of()
        Returns an unmodifiable list containing zero entries.
        Type Parameters:
        T - the List's value type.
        Returns:
        an empty List.
      • of

        public static <T> java.util.List<T> of​(T value)
        Returns an unmodifiable list containing a single entry.
        Type Parameters:
        T - the List's value type.
        Parameters:
        value - the List's value.
        Returns:
        a List containing the specified value.
        Throws:
        java.lang.NullPointerException - if the value is null.
      • of

        public static <T> java.util.List<T> of​(T value1,
                                               T value2)
        Returns an unmodifiable list containing two entries.
        Type Parameters:
        T - the List's value type.
        Parameters:
        value1 - The first value.
        value2 - The second value.
        Returns:
        a List containing the specified values.
      • of

        @SafeVarargs
        public static <T> java.util.List<T> of​(T... values)
        Returns an unmodifiable list containing any number of entries.
        Type Parameters:
        T - the List's value type.
        Parameters:
        values - the List's values.
        Returns:
        a List containing the specified values.
        Throws:
        java.lang.NullPointerException - if any value is null.
      • toUnmodifiableList

        public static <T> java.util.stream.Collector<T,​?,​java.util.List<T>> toUnmodifiableList()
        Creates a collector that collects into an unmodifiable list.

        This is a polyfill equivalent of Java 10's Collectors#toUnmodifiableList.

        Type Parameters:
        T - Type of value to expect.
        Returns:
        a Collector that accumulates the entries into an unmodifiable List.