Class RecursiveList<A>

Type Parameters:
A - the type of list elements
All Implemented Interfaces:
Nullfree, Iterable<A>, Collection<A>, List<A>, SequencedCollection<A>

public abstract class RecursiveList<A> extends AbstractSequentialList<A> implements Nullfree
An immutable singly-linked list implementation.

This implementation is generally less efficient than an array-based implementation. However, it has two major advantages:

  • Computing the rest of a list (containing all elements except the first one) executes in constant time and consumes no space.
  • Rest lists can be shared.

All usual list operations operate on constant stack space, even though their algebraic definitions may be recursive.

Since this implementation used singly-linked list cells, the operation ListIterator.previous() is significantly more costly that any other, and should be avoided. No penalty is incurred for list iterators where this method is never called.

This implementation does not support null elements.

Since:
1.4
  • Method Details

    • getFirst

      public abstract A getFirst()
      Returns the first element of this list if non-empty.
      Specified by:
      getFirst in interface List<A>
      Specified by:
      getFirst in interface SequencedCollection<A>
      Returns:
      the first element of this non-empty list
      Throws:
      NoSuchElementException - if this list is empty
    • getRest

      public abstract RecursiveList<? extends A> getRest()
      Returns the rest list of this list if non-empty.
      Returns:
      a list that contains all elements of this non-empty list except the first one
      Throws:
      NoSuchElementException - if this list is empty
    • cons

      public static <A> RecursiveList<A> cons(A first, RecursiveList<? extends A> rest)
      Returns a list that contains the given first element and rest list.
      Type Parameters:
      A - the type of list elements
      Parameters:
      first - the first element
      rest - the rest list
      Returns:
      a non-empty list that contains the element first followed by all of the list rest
      Throws:
      NullPointerException - if first or rest is null
    • empty

      public static <A> RecursiveList<A> empty()
      Returns an empty list.
      Type Parameters:
      A - the type of list elements
      Returns:
      a list that contains no elements
    • singleton

      public static <A> RecursiveList<A> singleton(A elem)
      Returns a list containing just the given element.
      Type Parameters:
      A - the type of the element
      Parameters:
      elem - the single element
      Returns:
      a list containing just the element elem
      Throws:
      NullPointerException - if elem is null
    • listIterator

      public ListIterator<A> listIterator(int index)
      Specified by:
      listIterator in interface List<A>
      Specified by:
      listIterator in class AbstractSequentialList<A>
    • get

      public A get(int index)
      Specified by:
      get in interface List<A>
      Overrides:
      get in class AbstractSequentialList<A>
    • of

      @SafeVarargs public static <A> RecursiveList<A> of(A... elems)
      Returns a list containing the given elements.

      The resulting list forever contains the elements of the given varargs array at the invocation time of this factory method. Subsequent changes to the array are not reflected.

      Type Parameters:
      A - the type of list elements
      Parameters:
      elems - the elements of the new list
      Returns:
      a list containing the given elements
      Throws:
      NullPointerException - if elems is or contains null
    • append

      public static <A> RecursiveList<A> append(RecursiveList<? extends A> left, RecursiveList<? extends A> right)
      Returns a list containing all elements of the given two lists in sequence.
      Type Parameters:
      A - the type of elements
      Parameters:
      left - one list of elements
      right - another list of elements
      Returns:
      a list that contains all elements of left followed by all elements of right
      Throws:
      NullPointerException - if left or right is null