Class ArraysUtils

java.lang.Object
net.paulem.arcana.utils.ArraysUtils

public class ArraysUtils extends Object
Utility methods for working with arrays and collections, such as picking random elements, computing differences, excluding elements, and reversing arrays.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> List<T>
    difference(List<T> a, List<T> b)
    Computes the set difference between two lists, i.e. the elements of a that are not present in b.
    static <T> T[]
    excludeElements(T[] array, Collection<T> toExclude)
    Creates a copy of the given array with all elements contained in toExclude removed.
    static <T> T
    getRandom(Collection<T> collection)
    Picks a random element from the given collection.
    static <T> T
    getRandom(List<T> list)
    Picks a random element from the given list.
    static <T> T
    getRandom(T[] array)
    Picks a random element from the given array.
    static <T> T[]
    invertArray(T[] array)
    Reverses the given array in place.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public static <T> T getRandom(List<T> list)
      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

      public static <T> T getRandom(Collection<T> collection)
      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

      public static <T> List<T> difference(List<T> a, List<T> b)
      Computes the set difference between two lists, i.e. the elements of a that are not present in b.
      Type Parameters:
      T - the type of the elements
      Parameters:
      a - the list to filter
      b - the list of elements to exclude
      Returns:
      a new list containing the elements of a that are not in b
    • excludeElements

      public static <T> T[] excludeElements(T[] array, Collection<T> toExclude)
      Creates a copy of the given array with all elements contained in toExclude removed.
      Type Parameters:
      T - the type of the elements
      Parameters:
      array - the array to filter
      toExclude - the elements to exclude from the result
      Returns:
      a new array containing only the elements of array that are not in toExclude
    • 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