Class FunctionalList

Object
FunctionalList

public class FunctionalList extends Object
Utility functions for manipulating or creating List in a functional way.
  • Method Details

    • of

      public static <T> List<T> of(Stream<T> stream)
      Creates a list from a stream.

      This function's purpose is mostly an convenience utility to make source-code easier to read, as the paradigm below (although very idiomatic) occurs frequently.

      Type Parameters:
      T - item-type
      Parameters:
      stream - the stream to create the list from
      Returns:
      the created list.
    • mapToList

      public static <S, T> List<T> mapToList(Stream<S> stream, Function<S,T> mapFunction)
      Maps a stream to a list with each element derived from a corresponding element in the original collection.

      This function's purpose is mostly an convenience utility to make source-code easier to read, as the paradigm below (although very idiomatic) occurs frequently.

      Type Parameters:
      S - parameter-type for function
      T - return-type for function
      Parameters:
      stream - the stream to be mapped
      mapFunction - function to do the mapping
      Returns:
      a list with the same size and same order, but using derived elements that are a result of the mapping
    • mapToList

      public static <S, T> List<T> mapToList(Collection<S> collection, Function<S,T> mapFunction)
      Maps a collection to a list with each element derived from a corresponding element in the original collection.

      This function's purpose is mostly an convenience utility to make source-code easier to read, as the paradigm below (although very idiomatic) occurs frequently.

      Type Parameters:
      S - parameter-type for function
      T - return-type for function
      Parameters:
      collection - the collection to be mapped.
      mapFunction - function to do the mapping.
      Returns:
      a list with the same size and same order, but using derived elements that are a result of the mapping.
    • mapToList

      public static <S, T, U> List<T> mapToList(Collection<S> collection, Collection<U> arguments, BiFunction<S,U,T> mapFunction)
      Maps a collection to a list with each element derived from a corresponding element in the original collection, as well as a corresponding argument from a second list.

      This function's purpose is mostly an convenience utility to make source-code easier to read, as the paradigm below (although very idiomatic) occurs frequently.

      Type Parameters:
      S - parameter-type for function
      T - return-type for function
      U - type of second argument for function
      Parameters:
      collection - the collection to be mapped.
      arguments - the argument to use together with element in collection.
      mapFunction - function to do the mapping.
      Returns:
      a list with the same size and same order, but using derived elements that are a result of the mapping.
    • mapToList

      public static <S, T> List<T> mapToList(S[] array, Function<S,T> mapFunction)
      Maps an array to a list with each element derived from a corresponding element in the original array.

      This function's purpose is mostly an convenience utility to make source-code easier to read, as the paradigm below (although very idiomatic) occurs frequently.

      Type Parameters:
      S - parameter-type for function
      T - return-type for function
      Parameters:
      array - the array to be mapped.
      mapFunction - function to do the mapping.
      Returns:
      a list with the same size and same order, but using derived elements that are a result of the mapping.
    • mapToList

      public static <S, T, E extends Exception> List<T> mapToList(Collection<S> collection, Class<? extends Exception> throwableClass, CheckedFunction<S,T,E> mapFunction) throws E
      Like mapToList(Object[], Function) but tolerates exceptions in the mapping function.
      Type Parameters:
      S - parameter-type for function
      T - return-type for function
      E - exception that can be thrown by @mapFunction
      Parameters:
      collection - the collection to be mapped.
      throwableClass - class type of exception that may be thrown by mapFunction.
      mapFunction - function to do the mapping.
      Returns:
      a list with the same size and same order, but using derived elements that are a result of the mapping
      Throws:
      E - if the exception is thrown during mapping
    • mapToList

      public static <S, T, E extends Exception> List<T> mapToList(S[] array, Class<? extends Exception> throwableClass, CheckedFunction<S,T,E> mapFunction) throws E
      Like mapToList(Object[], Function) but tolerates exceptions in the mapping function.
      Type Parameters:
      S - parameter-type for function
      T - return-type for function
      E - exception that can be thrown by mapFunction
      Parameters:
      array - the array to be mapped
      throwableClass - class type of exception that may be thrown by mapFunction.
      mapFunction - function to do the mapping.
      Returns:
      a list with the same size and same order, but using derived elements that are a result of the mapping.
      Throws:
      E - if the exception is thrown during mapping.
    • mapToList

      public static <S, T, E extends Exception> List<T> mapToList(Stream<S> stream, Class<? extends Exception> throwableClass, CheckedFunction<S,T,E> mapFunction) throws E
      Like mapToList(Stream, Function) but tolerates exceptions in the mapping function.
      Type Parameters:
      S - parameter-type for function
      T - return-type for function
      E - exception that can be thrown by @mapFunction
      Parameters:
      stream - the stream to be mapped.
      throwableClass - class type of exception that may be thrown by mapFunction.
      mapFunction - function to do the mapping.
      Returns:
      a list with the same size and same order, but using derived elements that are a result of the mapping.
      Throws:
      E - if the exception is thrown during mapping.
    • mapToListWithIndex

      public static <S, T> List<T> mapToListWithIndex(List<S> list, FunctionWithInt<S,T> mapFunction)
      Maps a collection to a list with each element derived from a corresponding element in the original collection - and also letting the map function use an index.
      Type Parameters:
      S - parameter-type for function
      T - return-type for function
      Parameters:
      list - the list to be mapped.
      mapFunction - function to do the mapping.
      Returns:
      a list with the same size and same order, but using derived elements that are a result of the mapping.
    • mapToListWithIndex

      public static <S, T, E extends Exception> List<T> mapToListWithIndex(List<S> list, Class<? extends Exception> throwableClass, CheckedFunctionWithInt<S,T,E> mapFunction) throws E
      Maps a collection to a list with each element derived from a corresponding element in the original collection - and also letting the map function use an index.
      Type Parameters:
      S - parameter-type for function
      T - return-type for function
      E - exception that can be thrown by @mapFunction
      Parameters:
      list - the list to be mapped.
      throwableClass - class type of exception that may be thrown by mapFunction.
      mapFunction - function to do the mapping.
      Returns:
      a list with the same size and same order, but using derived elements that are a result of the mapping.
      Throws:
      E - if the exception is thrown during mapping.
    • mapToListOptional

      public static <S, T> List<T> mapToListOptional(Collection<S> collection, Function<S,Optional<T>> mapFunction)
      Maps a collection to a list with each element in the original collection maybe producing an element in the output.
      Type Parameters:
      S - parameter-type for function
      T - return-type for function
      Parameters:
      collection - the collection to be mapped.
      mapFunction - function to do the mapping to an Optional (the item is included in the output if the optional is defined).
      Returns:
      a list with the same size and same order, but using derived elements that are a result of the mapping.
    • mapToListOptional

      public static <S, T, E extends Exception> List<T> mapToListOptional(Collection<S> collection, Class<? extends Exception> throwableClass, CheckedFunction<S,Optional<T>,E> mapFunction) throws E
      Maps a collection to a list with each element in the original collection maybe producing an element in the output.
      Type Parameters:
      S - parameter-type for function
      T - return-type for function
      E - an exception that may be thrown by an mapFunction
      Parameters:
      collection - the collection to be mapped.
      throwableClass - class type of exception that may be thrown by mapFunction.
      mapFunction - function to do the mapping to an Optional (the item is included in the output if the optional is defined).
      Returns:
      a list with the same size and same order, but using derived elements that are a result of the mapping.
      Throws:
      E - if it is thrown by any call to mapFunction
    • mapToListOptional

      public static <S, T, E extends Exception> List<T> mapToListOptional(Stream<S> stream, Class<? extends Exception> throwableClass, CheckedFunction<S,Optional<T>,E> mapFunction) throws E
      Maps a stream to a list with each element in the original collection maybe producing an element in the output.
      Type Parameters:
      S - parameter-type for function
      T - return-type for function
      E - an exception that may be thrown by an mapFunction
      Parameters:
      stream - the stream to be mapped.
      throwableClass - class type of exception that may be thrown by mapFunction.
      mapFunction - function to do the mapping to an Optional (the item is included in the output if the optional is defined).
      Returns:
      a list with the same size and same order, but using derived elements that are a result of the mapping.
      Throws:
      E - if it is thrown by any call to mapFunction
    • mapToListOptionalWithIndex

      public static <S, T, E extends Exception> List<T> mapToListOptionalWithIndex(List<S> list, Class<? extends Exception> throwableClass, CheckedFunctionWithInt<S,Optional<T>,E> mapFunction) throws E
      Maps a list to a new list with each element in the original collection maybe producing an element in the output.
      Type Parameters:
      S - parameter-type for function
      T - return-type for function
      E - an exception that may be thrown by an mapFunction
      Parameters:
      list - the list to be mapped.
      throwableClass - class type of exception that may be thrown by mapFunction.
      mapFunction - function to do the mapping to an Optional (the item is included in the output if the optional is defined).
      Returns:
      a list with the same size and same order, but using derived elements that are a result of the mapping.
      Throws:
      E - if it is thrown by any call to mapFunction
    • flatMapToList

      public static <S, T> List<T> flatMapToList(Collection<S> collection, Function<S,Stream<T>> mapFunction)
      Flat-maps a collection to a list where in the original collection can produce many elements in the outgoing list.
      Type Parameters:
      S - parameter-type for function
      T - return-type for function
      Parameters:
      collection - the collection to be mapped
      mapFunction - function to do the mapping
      Returns:
      a list with the same size and same order, but using derived elements that are a result of the mapping
    • flatMapToList

      public static <S, T, E extends Exception> List<T> flatMapToList(Collection<S> collection, Class<? extends Exception> throwableClass, CheckedFunction<S,Stream<? extends T>,E> mapFunction) throws E
      Flat-maps a collection to a list where in the original collection can produce many elements in the outgoing list.
      Type Parameters:
      S - parameter-type for function
      T - return-type for function
      E - exception that may be thrown by collection
      Parameters:
      collection - the collection to be mapped
      throwableClass - the class of E.
      mapFunction - function to do the mapping
      Returns:
      a list with the same size and same order, but using derived elements that are a result of the mapping
      Throws:
      E - if thrown by mapFunction
    • mapRangeToList

      public static <T> List<T> mapRangeToList(int startInclusive, int endExclusive, IntFunction<T> mapFunction)
      Creates a list of elements, where each element corresponds to an index in a range.
      Type Parameters:
      T - element-type in the list
      Parameters:
      startInclusive - minimum-element in range (inclusive).
      endExclusive - maximum-element in range (exclusive).
      mapFunction - function to do the mapping.
      Returns:
      a list with an element for every item in the range.
    • repeat

      public static <T> List<T> repeat(int numberRepeats, Supplier<T> mapFunction)
      Creates a list of elements, by repeatedly calling the same operation.
      Type Parameters:
      T - element-type in the list
      Parameters:
      numberRepeats - the number of repeats.
      mapFunction - function to create each element.
      Returns:
      a list with an element for every iteration.
    • filterToList

      public static <T> List<T> filterToList(Collection<T> collection, Predicate<T> predicate)
      Filters a collection and maps the result to a list.

      This function's purpose is mostly an convenience utility to make source-code easier to read, as the paradigm below (although idiomatic) occurs in multiple places.

      Type Parameters:
      T - list item-type
      Parameters:
      collection - the collection to be filtered.
      predicate - predicate to first filter the input collection before mapping.
      Returns:
      a list with only the elements that pass the filter.
    • filterToList

      public static <T, E extends Exception> List<T> filterToList(Collection<T> collection, Class<? extends Exception> throwableClass, CheckedPredicate<T,E> predicate) throws E
      Filters a collection and maps the result to a list.

      This function's purpose is mostly an convenience utility to make source-code easier to read, as the paradigm below (although idiomatic) occurs in multiple places.

      Type Parameters:
      T - list item-type
      E - exception that may be thrown during evaluating the predicate
      Parameters:
      collection - the collection to be filtered.
      throwableClass - class type of exception that may be thrown by mapFunction.
      predicate - predicate to first filter the input collection before mapping.
      Returns:
      a list with only the elements that pass the filter.
      Throws:
      E - if an exception is thrown during evaluating the predicate.
    • filterAndMapToList

      public static <S, T, E extends Exception> LinkedList<T> filterAndMapToList(List<S> list, CheckedPredicate<S,E> predicate, CheckedFunction<S,T,E> mapFunction) throws E
      Creates a new collection by filtering a list and then mapping to a list of another type.
      Type Parameters:
      S - type that will be mapped from
      T - type that will be mapped to
      E - exception that may be thrown during mapping
      Parameters:
      list - incoming list to be mapped.
      predicate - a condition that must be fulfilled for elements in list to be included in the returned list.
      mapFunction - function for mapping.
      Returns:
      a newly created list.
      Throws:
      E - if an exception is thrown while calling predicate or mapFunction.
    • filterAndMapWithIndexToList

      public static <S, T, E extends Exception> List<T> filterAndMapWithIndexToList(List<S> list, Predicate<S> predicate, CheckedFunctionWithInt<S,T,E> mapFuncWithIndex) throws E
      Creates a new collection by filtering a list and then mapping (with an index) to a list of another type.
      Type Parameters:
      S - type that will be mapped from
      T - type that will be mapped to
      E - exception that may be thrown during mapping
      Parameters:
      list - incoming list to be mapped
      predicate - a condition that must be fulfilled for elements in list to be included in the returned list.
      mapFuncWithIndex - function for mapping, also including an index (the original position in the bounding-box).
      Returns:
      a newly created list.
      Throws:
      E - if an exception is thrown during mapping.
    • zip

      public static <S, T> List<org.apache.commons.lang3.tuple.Pair<S,T>> zip(Collection<S> first, Collection<T> second)
      Create a list of pairs of elements from two separate collections.

      Elements are combined from each in the same order as iteration.

      Type Parameters:
      S - element-type in first collection
      T - element-type in second collection
      Parameters:
      first - first collection.
      second - second collection.
      Returns:
      a newly created list where each element is a pair comprising an element from first and the corresponding element from second.
    • zip

      public static <S, T, V> List<V> zip(Collection<S> first, Collection<T> second, BiFunction<S,T,V> combine)
      Create a list of elements from two separate collections.

      Elements are combined from each in the same order as iteration.

      Type Parameters:
      S - element-type in first collection
      T - element-type in second collection
      V - combined-element type
      Parameters:
      first - first collection.
      second - second collection.
      combine - how to combine a first and second element.
      Returns:
      a newly created list where each element is a pair comprising an element from first and the corresponding element from second.