Package ch.njol.util.coll
Class CollectionUtils
java.lang.Object
ch.njol.util.coll.CollectionUtils
Utils for collections and arrays. All methods will not print any errors for null collections/arrays, but will return false/-1/etc.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T[]array(T... array)Creates an array from the given objects.static booleancontains(@org.eclipse.jdt.annotation.Nullable int[] array, int num)static <T> booleancontains(@Nullable T[] array, @Nullable T o)static <T> booleancontainsAll(@Nullable T[] array, @Nullable T... os)static <T> booleancontainsAny(@Nullable T[] array, @Nullable T... os)static booleancontainsAnySuperclass(@Nullable Class<?>[] classes, @Nullable Class<?>... cs)static booleancontainsIgnoreCase(@Nullable String[] array, @Nullable String s)static <T, U> @Nullable Map.Entry<T,U>containsKey(@Nullable Map<T,U> map, @Nullable T key)containsKeyIgnoreCase(@Nullable Map<String,U> map, @Nullable String key)static booleancontainsSuperclass(@Nullable Class<?>[] classes, @Nullable Class<?> c)static <T> @Nullable Tstatic <T> @Nullable TgetRandom(@Nullable T[] os)static <T> @Nullable TgetRandom(@Nullable T[] os, int start)static intindexOf(@org.eclipse.jdt.annotation.Nullable int[] array, int num)static intindexOf(@org.eclipse.jdt.annotation.Nullable int[] array, int num, int start)static intindexOf(@org.eclipse.jdt.annotation.Nullable int[] array, int num, int start, int end)static <T> intFinds an object in an iterable usingObject.equals(Object).static <T> intindexOf(@Nullable T[] array, @Nullable T t)Finds an object in an array usingObject.equals(Object)(can find null elements).static <T> intindexOf(@Nullable T[] array, @Nullable T t, int start, int end)static intindexOfIgnoreCase(@Nullable Iterable<String> iter, @Nullable String s)Finds a string in a collection of strings (ignoring case).static intindexOfIgnoreCase(@Nullable String[] array, @Nullable String s)finds a string in an array of strings (ignoring case).static <E> Set<E>intersection(@Nullable Set<E>... sets)Gets the intersection of the given sets, i.e.static booleanstatic <T> intlastIndexOf(@Nullable T[] array, @Nullable T t)static byte[]permutation(byte start, byte end)Creates a permutation of all bytes in the interval [start, end]static int[]permutation(int length)Shorthand forpermutation(0, length - 1)static int[]permutation(int start, int end)Creates a permutation of all integers in the interval [start, end]static int[]toArray(@Nullable Collection<Integer> ints)Converts a collection of integers into a primitive int array.static float[]toFloats(@org.eclipse.jdt.annotation.Nullable double[] doubles)static <E> Set<E>Gets the union of the given sets, i.e.static Double[]wrap(double[] primitive)
-
Method Details
-
indexOf
public static <T> int indexOf(@Nullable T[] array, @Nullable T t)Finds an object in an array usingObject.equals(Object)(can find null elements).- Parameters:
array- The array to search ino- 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
finds a string in an array of strings (ignoring case).- Parameters:
array- the array to search ins- the string to search for- Returns:
- the index of the first occurrence of the given string or -1 if not found
-
containsIgnoreCase
-
indexOf
Finds an object in an iterable usingObject.equals(Object).- Parameters:
iter- The iterable to search ino- The object to search for- Returns:
- The index of the first occurrence of the given object or -1 if not found
-
indexOfIgnoreCase
Finds a string in a collection of strings (ignoring case).- Parameters:
iter- The iterable to search ins- 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
-
containsSuperclass
- Parameters:
classes- Array of classesc- 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 classescs- The classes to look for- Returns:
- Whether the classes or any of their superclasses are contained in the array
-
getRandom
public static <T> @Nullable T getRandom(@Nullable T[] os) -
getRandom
public static <T> @Nullable T getRandom(@Nullable T[] os, int start) -
getRandom
-
isSubset
- Parameters:
set- The set of elementssub- The set to test for being a subset of set- Returns:
- Whether sub only contains elements out of set or not
-
intersection
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
Gets the union of the given sets, i.e. a set that contains all elements of the given sets.- Parameters:
sets-- Returns:
-
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
-
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 permutationend- 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 permutationend- 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 forpermutation(0, length - 1) -
toArray
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
-