Package evo.search.util
Class ListUtils
- java.lang.Object
-
- evo.search.util.ListUtils
-
public class ListUtils extends Object
List utility methods.
-
-
Field Summary
Fields Modifier and Type Field Description static BinaryOperator<String>
REDUCE_WITH_SPACE
Reduce function to connect two strings via a space character.
-
Constructor Summary
Constructors Constructor Description ListUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
chooseRandom(List<T> list)
Choose a random item from the given list.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.static <T> void
consec(List<T> list, BiConsumer<T,T> consumer)
Look at a list of consecutive items in pairs of two.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.static <T> double
consecSum(List<T> list, BiFunction<T,T,Double> mapper)
Consecutively compute a sum over each pair of the list.static <T> List<T>
deepClone(List<T> items, UnaryOperator<T> cloner)
Deep clone a list of items.static <T> List<T>
from(Iterator<T> iterator)
Generate a list from an iterator.static <T> List<T>
generate(int limit, Supplier<T> supplier)
Generate aList
with a given length through a supplier.static float
getMinMaxRatio(float[] distances)
Compute the ratio of the maximum value divided by the minimum value of the float array.static <T,R>
List<R>map(List<T> list, Function<T,R> mapper)
List mapping.static double
mean(List<Double> numbers)
Computes the mean value of the list of doubles.static <T> String
printConsecutive(List<T> list, Function<T,String> mapper, String separator)
Prints list of objects in a consecutive order.static <T> T
reduce(List<T> list, BinaryOperator<T> reducer, T optional)
Reduce a list of items to a single item.static <T> List<T>
removeRepeating(List<T> list)
Remove repeating elements from a list.static BinaryOperator<String>
separator(String separator)
Returns a binary string reducing operator concatenating a collection of strings with a given separator.static double
sum(List<? extends Number> numbers)
Calculate the sum of the list of doubles.static <T> List<List<T>>
transpose(List<List<T>> matrix)
Transpose a two dimensional list matrix.static double
variance(List<? extends Number> list)
Calculates the variance for a list of numbers.
-
-
-
Field Detail
-
REDUCE_WITH_SPACE
public static final BinaryOperator<String> REDUCE_WITH_SPACE
Reduce function to connect two strings via a space character.
-
-
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 aList
with a given length through a supplier.- Type Parameters:
T
- list/supplier item type- Parameters:
limit
- length of the new listsupplier
- 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 frommin
- minimum indexmax
- 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 typeR
- mapping return and list item return type- Parameters:
list
- list of consecutive itemsmapping
- 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 itemsconsumer
- 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 togethermapper
- 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 printedmapper
- mapping function to map the objects to stringsseparator
- 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 clonecloner
- 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 reducereducer
- binary reducing operationoptional
- 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 typeR
- output element type- Parameters:
list
- list to mapmapper
- 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
-
-