Class CollectionUtils

java.lang.Object
ch.njol.util.coll.CollectionUtils

public abstract class CollectionUtils extends Object
Utils for collections and arrays. All methods will not print any errors for null collections/arrays, but will return false/-1/etc.
  • Method Details

    • indexOf

      public static <T> int indexOf(@Nullable T[] array, @Nullable T t)
      Finds an object in an array using Object.equals(Object) (can find null elements).
      Parameters:
      array - The array to search in
      o - The object to search for
      Returns:
      The index of the first occurrence of the given object or -1 if not found
    • lastIndexOf

      public static <T> int lastIndexOf(@Nullable T[] array, @Nullable T t)
    • indexOf

      public static <T> int indexOf(@Nullable T[] array, @Nullable T t, int start, int end)
    • contains

      public static <T> boolean contains(@Nullable T[] array, @Nullable T o)
    • containsAny

      public static <T> boolean containsAny(@Nullable T[] array, @Nullable T... os)
    • containsAll

      public static <T> boolean containsAll(@Nullable T[] array, @Nullable T... os)
    • indexOf

      public static int indexOf(@Nullable @org.jetbrains.annotations.Nullable int[] array, int num)
    • indexOf

      public static int indexOf(@Nullable @org.jetbrains.annotations.Nullable int[] array, int num, int start)
    • indexOf

      public static int indexOf(@Nullable @org.jetbrains.annotations.Nullable int[] array, int num, int start, int end)
    • contains

      public static boolean contains(@Nullable @org.jetbrains.annotations.Nullable int[] array, int num)
    • indexOfIgnoreCase

      public static int indexOfIgnoreCase(@Nullable @Nullable String[] array, @Nullable @Nullable String s)
      finds a string in an array of strings (ignoring case).
      Parameters:
      array - the array to search in
      s - the string to search for
      Returns:
      the index of the first occurrence of the given string or -1 if not found
    • containsIgnoreCase

      public static boolean containsIgnoreCase(@Nullable @Nullable String[] array, @Nullable @Nullable String s)
    • indexOf

      public static <T> int indexOf(@Nullable @Nullable Iterable<T> iter, @Nullable T o)
      Finds an object in an iterable using Object.equals(Object).
      Parameters:
      iter - The iterable to search in
      o - The object to search for
      Returns:
      The index of the first occurrence of the given object or -1 if not found
    • indexOfIgnoreCase

      public static int indexOfIgnoreCase(@Nullable @Nullable Iterable<String> iter, @Nullable @Nullable String s)
      Finds a string in a collection of strings (ignoring case).
      Parameters:
      iter - The iterable to search in
      s - The string to search for
      Returns:
      The index of the first occurrence of the given string or -1 if not found
    • containsKey

      @Nullable public static <T, U> @Nullable Map.Entry<T,U> containsKey(@Nullable @Nullable Map<T,U> map, @Nullable T key)
      Parameters:
      map -
      key -
      Returns:
      A new entry object or null if the key is not in the map
    • containsKeyIgnoreCase

      @Nullable public static <U> @Nullable Map.Entry<String,U> containsKeyIgnoreCase(@Nullable @Nullable Map<String,U> map, @Nullable @Nullable String key)
    • containsSuperclass

      public static boolean containsSuperclass(@Nullable @Nullable Class<?>[] classes, @Nullable @Nullable Class<?> c)
      Parameters:
      classes - Array of classes
      c - The class to look for
      Returns:
      Whether the class or any of its superclasses are contained in the array
    • containsAnySuperclass

      public static boolean containsAnySuperclass(@Nullable @Nullable Class<?>[] classes, @Nullable @Nullable Class<?>... cs)
      Parameters:
      classes - Array of classes
      cs - The classes to look for
      Returns:
      Whether the classes or any of their superclasses are contained in the array
    • isAnyInstanceOf

      public static boolean isAnyInstanceOf(Object object, Class<?>... classes)
      Returns:
      whether the given object is an instance of any of the given classes
    • getRandom

      @Nullable public static <T> T getRandom(@Nullable T[] os)
    • getRandom

      @Nullable public static <T> T getRandom(@Nullable T[] os, int start)
    • getRandom

      @Nullable public static <T> T getRandom(@Nullable @Nullable List<T> os)
    • isSubset

      public static boolean isSubset(@Nullable @Nullable Object[] set, @Nullable @Nullable Object[] sub)
      Parameters:
      set - The set of elements
      sub - The set to test for being a subset of set
      Returns:
      Whether sub only contains elements out of set or not
    • intersection

      public static <E> Set<E> intersection(@Nullable @Nullable Set<E>... sets)
      Gets the intersection of the given sets, i.e. a set that only contains elements that occur in all given sets.
      Parameters:
      sets -
      Returns:
    • union

      public static <E> Set<E> union(@Nullable @Nullable Set<E>... sets)
      Gets the union of the given sets, i.e. a set that contains all elements of the given sets.
      Parameters:
      sets -
      Returns:
    • array

      @SafeVarargs public static <T> T[] array(T... array)
      Creates an array from the given objects. Useful for creating arrays of generic types.

      The method is annotated

      invalid reference
      NonNull
      , but will simply return null if null is passed.
      Parameters:
      array - Some objects
      Returns:
      The passed array
    • arrayType

      public static <T> Class<T[]> arrayType(Class<T> c)
      Returns a Class for an array type whose component type is described by this Class.
      Returns:
      a Class describing the array type
    • subarray

      public static <T> T[] subarray(T[] array, int startIndex, int endIndex)
      Creates a new array whose elements are the elements between the start and end indices of the original array.
      Type Parameters:
      T - type of array
      Parameters:
      array - the original array
      startIndex - starting index (inclusive)
      endIndex - ending index (exclusive)
      Returns:
      a new array containing the elements between the start and end indices
    • permutation

      public static int[] permutation(int start, int end)
      Creates a permutation of all integers in the interval [start, end]
      Parameters:
      start - The lowest number which will be included in the permutation
      end - The highest number which will be included in the permutation
      Returns:
      an array of length end - start + 1, or an empty array if start > end.
    • permutation

      public static byte[] permutation(byte start, byte end)
      Creates a permutation of all bytes in the interval [start, end]
      Parameters:
      start - The lowest number which will be included in the permutation
      end - The highest number which will be included in the permutation
      Returns:
      an array of length end - start + 1, or an empty array if start > end.
    • permutation

      public static int[] permutation(int length)
      Shorthand for permutation(0, length - 1)
    • toArray

      public static int[] toArray(@Nullable @Nullable Collection<Integer> ints)
      Converts a collection of integers into a primitive int array.
      Parameters:
      ints - The collection
      Returns:
      An int[] containing the elements of the given collection in the order they were returned by the collection's iterator.
    • toFloats

      public static float[] toFloats(@Nullable @org.jetbrains.annotations.Nullable double[] doubles)
    • wrap

      public static Double[] wrap(double[] primitive)