Class PList<E>

java.lang.Object
ch.bluecare.commons.data.PList<E>
Type Parameters:
E -
All Implemented Interfaces:
Iterable<E>, IntFunction<E>
Direct Known Subclasses:
PList.Cons, PList.Nil

public abstract class PList<E> extends Object implements Iterable<E>, IntFunction<E>
A persistent singly linked list.
  • Method Details

    • nil

      public static <A> PList<A> nil()
      Return the empty list.

      It is the same list for all A.

    • empty

      public static <A> PList<A> empty()
      Returns the empty list.

      It is the same list for all A.

    • cell

      public static <A> PList<A> cell(A head, PList<A> tail)
      Create a “cons cell”, using the given head and tail.
    • single

      public static <A> PList<A> single(A element)
      Create a new single element list containing the given element.
    • fromNullable

      public static <A> PList<A> fromNullable(A element)
      Return either the empty list if element is null, or a singly element list with that element.

      Note that single(Object) would create a single-element list where the element is null.

    • generate

      public static <A, B> PList<B> generate(A init, Function<A,Optional<Pair<A,B>>> next)
      Generate a list by using the output of the given function. The function is called until it returns Optional.empty().
    • of

      @SafeVarargs public static <A> PList<A> of(A... elements)
      Create a list from the given elements in the given order.
    • fill

      public static <A> PList<A> fill(Supplier<A> supplier, int length)
      Create a list by calling the given supplier length times.
    • range

      public static PList<Integer> range(int start, int end)
      Creates a list of integers, from starte (inclusive) to end (exclusive).
    • fromIter

      public static <A> PList<A> fromIter(Iterable<A> iter)
      Create a list from an iterable, by traversing the iterable and cons-ing each element. The resulting list reflects the same order as the iterator of the given iterable emitted during traversal.
    • fromOptional

      public static <A> PList<A> fromOptional(Optional<A> opt)
      Create a single element or the empty list from a given Optional.
    • fromArray

      public static <A> PList<A> fromArray(A[] values)
      Create a list from the given array. It returns a list in same order as the given array. @param values.
    • collect

      public static <A> PList<A> collect(Iterator<A> iter)
      Traverse the iterator to create a new list.
    • collector

      public static <T> Collector<T,?,PList<T>> collector()
      A collector that can be used with Stream.collect(Collector) to collect a stream into a PList.
    • size

      public abstract int size()
      Return the size of this list. The size is cached and doesn't require to traverse the list.
    • tail

      public abstract PList<E> tail()
      Returns the tail of this list, that is without first element.
    • head

      public abstract E head()
      Return the first element (the head) of this list. An exception is thrown if this is the empty list.
    • isEmpty

      public abstract boolean isEmpty()
      Check whether this is the empty list.
    • apply

      public E apply(int index)
      Specified by:
      apply in interface IntFunction<E>
    • asFunction

      public Function<Integer,E> asFunction()
    • concat

      public PList<E> concat(PList<E> next)
      Concatenates the given list onto this list.
    • cons

      public final PList<E> cons(E element)
      Create a new list by adding element to the beginning of this list. That is, element becomes the head and this becomes the tail of the new list.
    • add

      public PList<E> add(E element)
      Create a new list by adding element to the tail of this list, i. e. the head of the list remains the same. Use this method carefully as it is expensive, use cons(Object) if possible.
    • contains

      public boolean contains(E element, BiPredicate<E,E> eq)
      Check whether the given element is a member of this list.
    • drop

      public final PList<E> drop(int count)
      Drop the first count elements of this list. If count is greater than the size of this list, the empty list is returned.
    • dropRight

      public final PList<E> dropRight(int count)
      Return a new list with the last count elements removed.
    • exists

      public boolean exists(Predicate<E> predicate)
      Check whether any element in this list holds the predicate.
    • filter

      public PList<E> filter(Predicate<E> filter)
      Return a new list with only those elements of this that holds the given predicate.
    • filterWithNext

      public PList<E> filterWithNext(BiPredicate<E,E> filter, Predicate<E> last)
      Filter this list by peeking the next element. The bi-predicate receives the current element as first argument and the next element as second argument.
    • filterWithPrev

      public PList<E> filterWithPrev(Predicate<E> first, BiPredicate<E,E> filter)
      Filter this list by peeking the previous element. The bi-predicate receives the current element as the second argument and the previous as first.
    • distinct

      public PList<E> distinct(Comparator<E> comparator)
      Remove duplicate elements. The returned list is sorted.

      A Comparator is used for determining whether two elements are a duplicate, because the list is first sorted, and then subsequent elements are compared, using the same comparator. If you can use equals/hashCode to compare elements, the distinct(Function) version is faster.

    • distinct

      public <T> PList<E> distinct(Function<E,T> getKey)
      Remove duplicate elements retaining the order. Duplicate elements are identified by using Object.equals(Object) on the computed key of an element with getKey. The first occurrence of an element is kept.

      If you want to specify a custom function to determine a duplicate, use distinct(Comparator) instead.

    • duplicates

      public PList<E> duplicates(Comparator<E> comparator)
      Retain only elements that exists more than once. The returned list is sorted and each duplicate is appears once.

      A Comparator is used for determining whether two elements are a duplicate, because the list is first sorted, and then subsequent elements are compared, using the same comparator.

    • find

      public Optional<E> find(Predicate<E> predicate)
      Find the first element that holds the given predicate.
    • flatMap

      public <B> PList<B> flatMap(Function<E,Iterable<B>> f)
      Apply f to each element in the list and concatenate the results.
    • flatMapOptional

      public <B> PList<B> flatMapOptional(Function<E,Optional<B>> f)
      Apply f to each element in the list and concatenate the non-empty results.
    • forall

      public boolean forall(Predicate<E> predicate)
      Check whether the given predicate holds for all elements in this list.
    • foldLeft

      public <B> B foldLeft(B init, BiFunction<B,E,B> f)
      Folds the list from the left.

      The function f is applied to the init element and the first element of this list. Then f is applied to the result and the second element in this list, and so on.

      If this list is empty, the init element is returned and f is not invoked.

      The list is alwayes completely traversed.

      Parameters:
      init - the initial element
      f - the function merging each element to the result of f
    • foldRight

      public <B> B foldRight(B init, BiFunction<B,E,B> f)
      Same as foldLeft(Object, BiFunction) but going from the right (the end of the list).

      Note that this version is not recursive and therefore is stack-safe but lacks the early-return feature.

      Parameters:
      init - the initial element of the fold
      f - the function merging the result of f with each element
    • groupBy

      public <K> Map<K,NonEmptyList<E>> groupBy(Function<E,K> f)
      Return a map of lists that are keyed by f.
    • headOption

      public final Optional<E> headOption()
      Returns the head of this list or Optional.empty() if this is the empty list.

      Note: If the element exists but is null, then Optional.empty() is returned as well!

    • indexOf

      public int indexOf(E element, BiPredicate<E,E> eq)
      Returns the first index of the given element or -1 if not found.
    • indexesOf

      public PList<Integer> indexesOf(E element, BiPredicate<E,E> eq)
      Return all indexes of the given element.
    • iterator

      public Iterator<E> iterator()
      Specified by:
      iterator in interface Iterable<E>
    • map

      public <B> PList<B> map(Function<E,B> f)
      Apply f to each element in this list, returning a new list preserving this structure.
    • max

      public Optional<E> max(Comparator<E> comparator)
      Return the maximum element according to the given comparator.
    • min

      public Optional<E> min(Comparator<E> comparator)
      Return the minimum element according to the given comparator.
    • mkString

      public String mkString(String sep)
      Create a string where sep appears between elements. Elements are added using their toString() method.
    • nonEmpty

      public final boolean nonEmpty()
      Check whether the list is not empty.
    • reduce

      public Optional<E> reduce(BinaryOperator<E> f)
      Reduces this list to a single value using the merge function f.
    • reverse

      public PList<E> reverse()
      Reverse the order of this list.
    • take

      public final PList<E> take(int count)
      Take the first count elements from this list.
    • takeRight

      public final PList<E> takeRight(int count)
      Return a new list with only the last count elements of this list.
    • sort

      public PList<E> sort(Comparator<E> comparator)
      Return a new sorted version of this list.
    • toArrayList

      public List<E> toArrayList()
      Create a new mutable ArrayList from this persistent list.
    • toArray

      public E[] toArray(Class<E> type)
      Return a new array containing the elements of this list.
    • toByteArray

      public static byte[] toByteArray(PList<Byte> list)
      Create a new primitive array of the given list.
    • toIntArray

      public static int[] toIntArray(PList<Integer> list)
      Create a new primitive array of the given list.
    • toLongArray

      public static long[] toLongArray(PList<Integer> list)
      Create a new primitive array of the given list.
    • toFloatArray

      public static float[] toFloatArray(PList<Integer> list)
      Create a new primitive array of the given list.
    • toDoubleArray

      public static double[] toDoubleArray(PList<Integer> list)
      Create a new primitive array of the given list.
    • toHashSet

      public Set<E> toHashSet()
      Create a mutable HashSet from this persistent list.
    • toStream

      public Stream<E> toStream()
      Create a Stream from this list.
    • zipLazy

      public <B> Iterable<Pair<E,B>> zipLazy(Iterable<B> other)
    • zip

      public <A> PList<Pair<E,A>> zip(Iterable<A> other)
    • zipWithIndex

      public PList<Pair<E,Integer>> zipWithIndex()
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object