Class List<T>

java.lang.Object
me.sbasalaev.collection.Collection<T>
me.sbasalaev.collection.List<T>
All Implemented Interfaces:
Cloneable, Iterable<T>, Traversable<T>
Direct Known Subclasses:
MutableList

public abstract class List<@Out T> extends Collection<T>
Sequence of elements numbered by integer indices. An implementation of the list must provide get() and size(). The default iterator implementation uses these methods.
  • Constructor Details

    • List

      public List()
      Constructor for subclasses.
  • Method Details

    • empty

      public static <T> List<T> empty()
      Empty list.
    • of

      @SafeVarargs public static <T> List<T> of(T... elements)
      List of given elements.
    • concatenated

      @SafeVarargs public static <T> List<T> concatenated(List<? extends T>... lists)
      Concatenates several lists together.
    • fromJava

      public static <T> List<T> fromJava(List<T> javaList)
      List view of given Java list.
    • repeat

      public static <T> List<T> repeat(T element, int times)
      List that repeats the same element given number of times.
    • get

      public abstract T get(int index)
      Element at the specified index of the list.
      Throws:
      IndexOutOfBoundsException - if index is negative or ≥ Collection.size().
    • lastIndex

      public int lastIndex()
      The last valid index in this list.
      Returns:
      size()-1.
    • first

      public T first() throws NoSuchElementException
      The first element of this list.
      Throws:
      NoSuchElementException - if the list is empty.
      See Also:
    • last

      public T last() throws NoSuchElementException
      The last element of this list.
      Throws:
      NoSuchElementException - if the list is empty.
      See Also:
    • findIndex

      public int findIndex(Predicate<? super @Out T> condition)
      The index of the first element satisfying given condition. Returns -1 if there is no such element.
    • findIndex

      public int findIndex(Predicate<? super @Out T> condition, int fromIndex)
      The index of the first element satisfying given condition. Returns -1 if there is no such element.
    • findLastIndex

      public int findLastIndex(Predicate<? super @Out T> condition)
      The index of the last element satisfying given condition. Returns -1 if there is no such element.
    • binarySearch

      public int binarySearch(ToIntFunction<@Out T> comparator)
      Finds item using binary search assuming elements are in increasing order.
    • indexed

      public List<IndexedElement<T>> indexed()
      A view of this list where each element is coupled with the corresponding index. The returned list is a view that is affected immediately by the changes to this list.
    • forEachIndexed

      public void forEachIndexed(ObjIntConsumer<? super @Out T> action)
      Performs given action for each element of this list. The consumer for this method accepts item index as the second argument.
      Since:
      4.0
    • pairs

      public <R> Traversable<R> pairs(BiFunction<? super @Out T,? super @Out T,? extends R> combiner)
      Traverses all pairs of elements in this list. The resulting traversable returns elements produced by
      combiner.apply(get(i), get(j))
      for all indices i, j such that
      0 <= i < j < size()
      In particular, the traversable is empty if the list has less than two elements.
      Since:
      4.0
    • forEachPair

      public void forEachPair(BiConsumer<? super @Out T,? super @Out T> action)
      Performs given action for all pairs of elements in this list. The action is called as
      action.accept(get(i), get(j))
      for all indices i, j such that
      0 <= i < j < size()
      In particular, no action is performed if the list has less than two elements.
      Since:
      4.0
    • from

      public List<T> from(int offset)
      A view of this list that contains only elements starting from given offset. If this list contains no more than offset elements the returned view is empty. The returned list is a view that is affected immediately by the changes to this list.
      Parameters:
      offset - non-negative offset from the start of the list.
      Throws:
      IllegalArgumentException - if the offset is negative.
    • take

      public List<T> take(int limit)
      A view of this list that contains no more than limit elements. The returned list is a view that is affected immediately by the changes to this list.
      Parameters:
      limit - maximum number of elements to take.
      Throws:
      IllegalArgumentException - if given limit is negative.
      See Also:
    • reversed

      public List<T> reversed()
      A view of this list that contains the same elements in the reversed order. The returned list is a view that is affected immediately by the changes to this list.
    • zip

      public <U, R> List<R> zip(List<U> other, BiFunction<? super @Out T,? super U,? extends R> zipper)
      A combined view of this and other lists using given zipper. The returned list is a view that is affected immediately by the changes to this and other lists. Size of the view is the minimum of sizes of two lists and the element at each index is a combination by zipper of the elements of this and other at the same index.
    • toJava

      public List<T> toJava()
      A view of this list as Java list.
    • iterator

      public Iterator<T> iterator()
      Iterator of this list. The iterator returns elements of this list in order, starting at index 0.

      The default implementation of this method returns the iterator that is not aware of the changes made to this list.

    • spliterator

      public Spliterator<T> spliterator()
      Creates a Spliterator over elements of this list. The spliterator reports Spliterator.NONNULL, Spliterator.SIZED and Spliterator.ORDERED.
      Specified by:
      spliterator in interface Iterable<T>
      Specified by:
      spliterator in class Collection<T>
    • clone

      public final List<T> clone()
      Returns shallow immutable copy of this list. May return the same instance if the list is immutable.
      Specified by:
      clone in class Collection<T>
    • map

      public <R> List<R> map(Function<? super @Out T,? extends R> mapping)
      Returns view of this list with given mapping applied to all elements. The returned list is a view that is affected immediately by the changes to this list.
      See Also:
    • mapped

      public <R> List<R> mapped(Function<? super @Out T,? extends R> mapping)
      Returns new list with given mapping applied to all elements. The returned list is immutable and is unaffected by the changes to this list.
      Specified by:
      mapped in class Collection<T>
    • filtered

      public List<T> filtered(Predicate<? super @Out T> condition)
      Returns new list that contains only elements of this list satisfying given condition. The returned list is immutable and is unaffected by the changes to this list.
      Specified by:
      filtered in class Collection<T>
    • equals

      public boolean equals(@Nullable Object obj)
      Compares two objects for equality. Two lists are equal if they have the same size and the corresponding pairs of elements are equal.
      Specified by:
      equals in class Collection<T>
    • hashCode

      public int hashCode()
      Returns hash code for the list. The hash code for the list is defined to be equal to
      fold(1, (hash, item) -> hash*31 + item.hashCode());
      This means that the hash code for the list is compatible with Java definitions for List.hashCode(), Arrays.hashCode(java.lang.Object[]) and Objects.hash(java.lang.Object...).
      Specified by:
      hashCode in class Collection<T>