Package net.paulem.arcana.utils
Class ArraysUtils
java.lang.Object
net.paulem.arcana.utils.ArraysUtils
Utility methods for working with arrays and collections, such as picking random
elements, computing differences, excluding elements, and reversing arrays.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> List<T> difference(List<T> a, List<T> b) Computes the set difference between two lists, i.e. the elements ofathat are not present inb.static <T> T[]excludeElements(T[] array, Collection<T> toExclude) Creates a copy of the given array with all elements contained intoExcluderemoved.static <T> TgetRandom(Collection<T> collection) Picks a random element from the given collection.static <T> TPicks a random element from the given list.static <T> TgetRandom(T[] array) Picks a random element from the given array.static <T> T[]invertArray(T[] array) Reverses the given array in place.
-
Constructor Details
-
ArraysUtils
public ArraysUtils()
-
-
Method Details
-
getRandom
public static <T> T getRandom(T[] array) Picks a random element from the given array.- Type Parameters:
T- the type of the elements- Parameters:
array- the array to pick from- Returns:
- a randomly chosen element of the array
-
getRandom
Picks a random element from the given list.- Type Parameters:
T- the type of the elements- Parameters:
list- the list to pick from- Returns:
- a randomly chosen element of the list
-
getRandom
Picks a random element from the given collection. The collection is copied into a list before an element is chosen.- Type Parameters:
T- the type of the elements- Parameters:
collection- the collection to pick from- Returns:
- a randomly chosen element of the collection
-
difference
Computes the set difference between two lists, i.e. the elements ofathat are not present inb.- Type Parameters:
T- the type of the elements- Parameters:
a- the list to filterb- the list of elements to exclude- Returns:
- a new list containing the elements of
athat are not inb
-
excludeElements
Creates a copy of the given array with all elements contained intoExcluderemoved.- Type Parameters:
T- the type of the elements- Parameters:
array- the array to filtertoExclude- the elements to exclude from the result- Returns:
- a new array containing only the elements of
arraythat are not intoExclude
-
invertArray
public static <T> T[] invertArray(T[] array) Reverses the given array in place.- Type Parameters:
T- the type of the elements- Parameters:
array- the array to reverse- Returns:
- the same array instance, with its elements in reversed order
-