Class FList<E>

  • Type Parameters:
    E - The type of elements
    All Implemented Interfaces:
    Serializable, Iterable<E>
    Direct Known Subclasses:
    FList.Cons, FList.Nil

    public abstract class FList<E>
    extends Object
    implements Iterable<E>, Serializable
    Generic functional list implementation. Functional lists are immutable. Elements can be added in front of a functional list and a new list with the new element as head and this list as tail is returned.
    Author:
    Herbert Praehofer
    See Also:
    Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  FList.Builder<E>
      Mutable builder for building functional lists.
      static class  FList.Cons<E>
      Class representing a functional list with a head element and tail list.
      static class  FList.Nil<E>
      Class for the empty list.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static FList NIL
      The single object representing the empty list
    • Constructor Summary

      Constructors 
      Constructor Description
      FList()  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      FList<E> add​(E elem)
      Creates a new list with the given element as head and this list as tail.
      boolean contains​(Object o)
      Tests if the provided object is contained in this list.
      static <E> FList<E> empty()
      Returns the empty list.
      FList<E> filter​(Predicate<? super E> predicate)
      Returns a functional list consisting of the elements of this list that match the given predicate.
      Optional<E> find​(Predicate<? super E> pred)
      Finds an element that fulfills the given predicate.
      <R> FList<R> flatMap​(Function<? super E,​? extends FList<? extends R>> mapper)
      Returns a functional list consisting of the results of replacing each element of this list with the contents of a mapped list produced by applying the provided mapping function to each element.
      void forEach​(Consumer<? super E> action)
      Performs an action for each element of this list.
      static <T> FList<T> generate​(int n, Supplier<T> supplier)
      Creates a functional list with n elements generated by the provided supplier function.
      E get​(int idx)
      Gets the element at index idx.
      abstract E head()
      Returns the head element of this list.
      abstract boolean isEmpty()
      Tests if this list is the empty list.
      Iterator<E> iterator()
      Returns an iterator for this list.
      <R> FList<R> map​(Function<? super E,​? extends R> mapper)
      Returns a functional list consisting of the results of applying the given function to the elements of this list.
      static <E> FList<E> of​(E... elems)
      Creates a functional list with the given elements.
      Stream<E> parallelStream()
      Creates a parallel stream from this functional list.
      E reduce​(E identity, BinaryOperator<E> combiner)
      Performs a reduction on the elements of this list starting with provided identity value and using the provided combiner operator.
      <R> R reduce​(R identity, BiFunction<? super R,​? super E,​? extends R> acc)
      Performs a reduction on the elements of this list starting with provided identity value and using the provided accumulation function.
      FList<E> remove​(Object obj)
      Creates a new functional list with same elements as this list but without the given object.
      FList<E> removeIdx​(int idx)
      Creates a new functional list with same elements as this list but with the element at index idx removed.
      private FList<E> removeRec​(FList<E> fList, Object obj)
      Helper function for remove elements.
      FList<E> replaceIdx​(int idx, E elem)
      Creates a new functional list with same elements as this list but with the element at position idx replaced by the given element.
      FList<E> reverse()
      Creates a new functional list with the elements reversed.
      abstract int size()
      Returns the size of this list.
      Stream<E> stream()
      Creates a sequential stream from this functional list.
      abstract FList<E> tail()
      Returns the tail list of this list.
      Object[] toArray()
      Creates an object array with the elements of this list.
      E[] toArray​(E[] a)
      Creates an array with the elements of this list.
      String toString()
      Returns a string representation of this list.
    • Field Detail

      • NIL

        private static FList NIL
        The single object representing the empty list
    • Constructor Detail

      • FList

        public FList()
    • Method Detail

      • empty

        public static <E> FList<E> empty()
        Returns the empty list.
        Type Parameters:
        E - the element type
        Returns:
        the empty list
      • of

        public static <E> FList<E> of​(E... elems)
        Creates a functional list with the given elements.
        Type Parameters:
        E - the element type
        Parameters:
        elems - the array of elements for this list
        Returns:
        the functional list with the given elements
      • add

        public FList<E> add​(E elem)
        Creates a new list with the given element as head and this list as tail.
        Parameters:
        elem - the first element of the created list
        Returns:
        the functional list with the given element as head and this list as tail
      • isEmpty

        public abstract boolean isEmpty()
        Tests if this list is the empty list.
        Returns:
        true, if this list is the empty list; false otherwise
      • size

        public abstract int size()
        Returns the size of this list.
        Returns:
        the size of this list
      • head

        public abstract E head()
        Returns the head element of this list.
        Returns:
        the head element
      • tail

        public abstract FList<E> tail()
        Returns the tail list of this list.
        Returns:
        the tail list
      • contains

        public boolean contains​(Object o)
        Tests if the provided object is contained in this list.
        Parameters:
        o - the object to look for
        Returns:
        true if object found, false otherwise
      • get

        public E get​(int idx)
        Gets the element at index idx. Throws an IndexOutOfBoundsException if the index is out of range.
        Parameters:
        idx - the position of the element
        Returns:
        the element at position idx
      • remove

        public FList<E> remove​(Object obj)
        Creates a new functional list with same elements as this list but without the given object.
        Parameters:
        obj - the object to remove
        Returns:
        a new functional list without the given object
      • removeIdx

        public FList<E> removeIdx​(int idx)
        Creates a new functional list with same elements as this list but with the element at index idx removed.
        Parameters:
        idx - the index of the element to remove
        Returns:
        a new functional list without the element at index idx
      • replaceIdx

        public FList<E> replaceIdx​(int idx,
                                   E elem)
        Creates a new functional list with same elements as this list but with the element at position idx replaced by the given element.
        Parameters:
        idx - the index of the element to replace
        elem - the new element
        Returns:
        a new functional list without the element at index idx replaced by the new element
      • iterator

        public Iterator<E> iterator()
        Returns an iterator for this list.
        Specified by:
        iterator in interface Iterable<E>
        Returns:
        the iterator for this list
      • reverse

        public FList<E> reverse()
        Creates a new functional list with the elements reversed.
        Returns:
        a new functional list with the elements reversed
      • toArray

        public Object[] toArray()
        Creates an object array with the elements of this list.
        Returns:
        an object array with the elements of this list
      • toArray

        public E[] toArray​(E[] a)
        Creates an array with the elements of this list. The runtime type of the returned array is that of the given array.
        Parameters:
        a - the array with the same type as the returned array
        Returns:
        the array with the elements of this list
      • toString

        public String toString()
        Returns a string representation of this list.
        Overrides:
        toString in class Object
        Returns:
        a string representation of this list
      • map

        public <R> FList<R> map​(Function<? super E,​? extends R> mapper)
        Returns a functional list consisting of the results of applying the given function to the elements of this list.
        Type Parameters:
        R - The element type of the new list
        Parameters:
        mapper - a function to apply to each element
        Returns:
        the new list with the mapped elements
      • filter

        public FList<E> filter​(Predicate<? super E> predicate)
        Returns a functional list consisting of the elements of this list that match the given predicate.
        Parameters:
        predicate - a predicate to apply to each element to determine if it should be included
        Returns:
        the new list with the elements filtered
      • flatMap

        public <R> FList<R> flatMap​(Function<? super E,​? extends FList<? extends R>> mapper)
        Returns a functional list consisting of the results of replacing each element of this list with the contents of a mapped list produced by applying the provided mapping function to each element.
        Type Parameters:
        R - The element type of the new list
        Parameters:
        mapper - a function to apply to each element which produces a list of new values
        Returns:
        the new list with the mapped elements
      • forEach

        public void forEach​(Consumer<? super E> action)
        Performs an action for each element of this list.
        Specified by:
        forEach in interface Iterable<E>
        Parameters:
        action - an function to perform on the elements
      • reduce

        public <R> R reduce​(R identity,
                            BiFunction<? super R,​? super E,​? extends R> acc)
        Performs a reduction on the elements of this list starting with provided identity value and using the provided accumulation function.
        Type Parameters:
        R - The type of the result
        Parameters:
        identity - the identity value for the combiner function
        acc - the accumulation function
        Returns:
        the result of the reduction
      • reduce

        public E reduce​(E identity,
                        BinaryOperator<E> combiner)
        Performs a reduction on the elements of this list starting with provided identity value and using the provided combiner operator. The result if the reduction is of the same type as the type of the elements. A binary operation is used for reduction.
        Parameters:
        identity - the identity value for the combiner function
        combiner - the combiner operator
        Returns:
        the result of the reduction
      • generate

        public static <T> FList<T> generate​(int n,
                                            Supplier<T> supplier)
        Creates a functional list with n elements generated by the provided supplier function.
        Type Parameters:
        T - the type of the elements
        Parameters:
        n - the number of elements
        supplier - the supplier function for creating the elements
        Returns:
        the functional list with the generated elements
      • stream

        public Stream<E> stream()
        Creates a sequential stream from this functional list.
        Returns:
        the stream object
        See Also:
        Stream
      • parallelStream

        public Stream<E> parallelStream()
        Creates a parallel stream from this functional list.
        Returns:
        the stream object
        See Also:
        Stream
      • removeRec

        private FList<E> removeRec​(FList<E> fList,
                                   Object obj)
        Helper function for remove elements.
        Parameters:
        fList - the list to remove the element
        obj - the object to remove
        Returns:
        the functional list with the element removed