Class ListUtils


  • public class ListUtils
    extends Object
    List utility methods.
    • Field Detail

      • REDUCE_WITH_SPACE

        public static final BinaryOperator<String> REDUCE_WITH_SPACE
        Reduce function to connect two strings via a space character.
    • Constructor Detail

      • ListUtils

        public ListUtils()
    • Method Detail

      • separator

        public static BinaryOperator<String> separator​(String separator)
        Returns a binary string reducing operator concatenating a collection of strings with a given separator.
        Parameters:
        separator - separator to place between each pair of strings
        Returns:
        concatenated string of the collection's string separated by the given separator
      • generate

        public static <T> List<T> generate​(int limit,
                                           Supplier<T> supplier)
        Generate a List with a given length through a supplier.
        Type Parameters:
        T - list/supplier item type
        Parameters:
        limit - length of the new list
        supplier - supplier to retrieve the items from
        Returns:
        a list with element generated by the given supplier
      • chooseRandom

        public static <T> T chooseRandom​(List<T> list,
                                         int min,
                                         int max)
        Choose a random item from the given list in the index range between min and max.
        Type Parameters:
        T - list element and return type
        Parameters:
        list - list to choose from
        min - minimum index
        max - maximum index
        Returns:
        random element from the list between the indices min and max
      • chooseRandom

        public static <T> T chooseRandom​(List<T> list)
        Choose a random item from the given list.
        Type Parameters:
        T - list element and return type
        Parameters:
        list - list to choose from
        Returns:
        random element from the list
      • consecMap

        public static <T,​R> List<R> consecMap​(List<T> list,
                                                    BiFunction<T,​T,​R> mapping)
        Map a list of consecutive items in pairs of two to a new list.
        Type Parameters:
        T - list item type
        R - mapping return and list item return type
        Parameters:
        list - list of consecutive items
        mapping - mapping function
        Returns:
        list of mapped together elements
      • consec

        public static <T> void consec​(List<T> list,
                                      BiConsumer<T,​T> consumer)
        Look at a list of consecutive items in pairs of two.
        Type Parameters:
        T - list item type
        Parameters:
        list - list of consecutive items
        consumer - consumer working with two consecutive items
      • consecSum

        public static <T> double consecSum​(List<T> list,
                                           BiFunction<T,​T,​Double> mapper)
        Consecutively compute a sum over each pair of the list.
        Type Parameters:
        T - type of list items
        Parameters:
        list - list to pair together
        mapper - mapping to fuse two items into a double
        Returns:
        sum of the mapped doubles
      • from

        public static <T> List<T> from​(Iterator<T> iterator)
        Generate a list from an iterator.
        Type Parameters:
        T - element type
        Parameters:
        iterator - iterator supplying elements
        Returns:
        list of elements supplied by the iterator
      • transpose

        public static <T> List<List<T>> transpose​(List<List<T>> matrix)
        Transpose a two dimensional list matrix.
        Type Parameters:
        T - element type of the list
        Parameters:
        matrix - two dimensional list
        Returns:
        transposed, two dimensional list matrix
      • printConsecutive

        public static <T> String printConsecutive​(List<T> list,
                                                  Function<T,​String> mapper,
                                                  String separator)
        Prints list of objects in a consecutive order.
        Type Parameters:
        T - list item type
        Parameters:
        list - list of objects to be printed
        mapper - mapping function to map the objects to strings
        separator - item string separator
        Returns:
        string of printed list
      • variance

        public static double variance​(List<? extends Number> list)
        Calculates the variance for a list of numbers.
        Parameters:
        list - list of numbers
        Returns:
        variance of the values
      • removeRepeating

        public static <T> List<T> removeRepeating​(List<T> list)
        Remove repeating elements from a list.
        Type Parameters:
        T - type of list items
        Parameters:
        list - list with repeating elements
        Returns:
        list without items repeating
      • deepClone

        public static <T> List<T> deepClone​(List<T> items,
                                            UnaryOperator<T> cloner)
        Deep clone a list of items.
        Type Parameters:
        T - type of items
        Parameters:
        items - items to clone
        cloner - cloning method
        Returns:
        list of deeply cloned items
      • sum

        public static double sum​(List<? extends Number> numbers)
        Calculate the sum of the list of doubles.
        Parameters:
        numbers - list of doubles
        Returns:
        sum of list of doubles
      • reduce

        public static <T> T reduce​(List<T> list,
                                   BinaryOperator<T> reducer,
                                   T optional)
        Reduce a list of items to a single item.
        Type Parameters:
        T - type of list items and return value
        Parameters:
        list - list of items to reduce
        reducer - binary reducing operation
        optional - starting value of reduction, gets returned if list is empty
        Returns:
        result of list reduction
      • mean

        public static double mean​(List<Double> numbers)
        Computes the mean value of the list of doubles.
        Parameters:
        numbers - list of doubles
        Returns:
        mean of list of doubles
      • map

        public static <T,​R> List<R> map​(List<T> list,
                                              Function<T,​R> mapper)
        List mapping. Elements mapped to null are ignored.
        Type Parameters:
        T - input element type
        R - output element type
        Parameters:
        list - list to map
        mapper - mapping function
        Returns:
        list of mapped elements
      • getMinMaxRatio

        public static float getMinMaxRatio​(float[] distances)
        Compute the ratio of the maximum value divided by the minimum value of the float array.
        Parameters:
        distances - list of float values
        Returns:
        max value divided by min value