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(@org.eclipse.jdt.annotation.Nullable int[] array, int num)
    • indexOf

      public static int indexOf(@org.eclipse.jdt.annotation.Nullable int[] array, int num, int start)
    • indexOf

      public static int indexOf(@org.eclipse.jdt.annotation.Nullable int[] array, int num, int start, int end)
    • contains

      public static boolean contains(@org.eclipse.jdt.annotation.Nullable int[] array, int num)
    • indexOfIgnoreCase

      public static int indexOfIgnoreCase(@Nullable String[] array, @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 String[] array, @Nullable String s)
    • indexOf

      public static <T> int indexOf(@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 Iterable<String> iter, @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

      public static <T, U> @Nullable Map.Entry<T,U> containsKey(@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

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

      public static boolean containsSuperclass(@Nullable Class<?>[] classes, @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 Class<?>[] classes, @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

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

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

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

      public static boolean isSubset(@Nullable Object[] set, @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 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 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 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
    • 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 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(@org.eclipse.jdt.annotation.Nullable double[] doubles)
    • wrap

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