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 <T> Class
<T[]> Returns aClass
for an array type whose component type is described by this Class.static boolean
contains
(@org.jetbrains.annotations.Nullable int[] array, int num) static <T> boolean
contains
(T[] array, T o) static <T> boolean
containsAll
(T[] array, T... os) static <T> boolean
containsAny
(T[] array, T... os) static boolean
containsAnySuperclass
(@Nullable Class<?>[] classes, @Nullable Class<?>... cs) static boolean
containsIgnoreCase
(@Nullable String[] array, @Nullable String s) static <T,
U> @Nullable Map.Entry <T, U> containsKey
(@Nullable Map<T, U> map, T key) containsKeyIgnoreCase
(@Nullable Map<String, U> map, @Nullable String key) static boolean
containsSuperclass
(@Nullable Class<?>[] classes, @Nullable Class<?> c) static <T> T
static <T> T
getRandom
(T[] os) static <T> T
getRandom
(T[] os, int start) static int
indexOf
(@org.jetbrains.annotations.Nullable int[] array, int num) static int
indexOf
(@org.jetbrains.annotations.Nullable int[] array, int num, int start) static int
indexOf
(@org.jetbrains.annotations.Nullable int[] array, int num, int start, int end) static <T> int
Finds an object in an iterable usingObject.equals(Object)
.static <T> int
indexOf
(T[] array, T t) Finds an object in an array usingObject.equals(Object)
(can find null elements).static <T> int
indexOf
(T[] array, T t, int start, int end) static int
indexOfIgnoreCase
(@Nullable Iterable<String> iter, @Nullable String s) Finds a string in a collection of strings (ignoring case).static int
indexOfIgnoreCase
(@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 boolean
isAnyInstanceOf
(Object object, Class<?>... classes) static boolean
static <T> int
lastIndexOf
(T[] array, 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 <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.static int[]
toArray
(@Nullable Collection<Integer> ints) Converts a collection of integers into a primitive int array.static float[]
toFloats
(@org.jetbrains.annotations.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(@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 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
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 ins
- 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
-
containsSuperclass
public static boolean containsSuperclass(@Nullable @Nullable Class<?>[] classes, @Nullable @Nullable Class<?> c) - 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 @Nullable Class<?>[] classes, @Nullable @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
-
isAnyInstanceOf
- 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
-
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
invalid reference
NonNull
- Parameters:
array
- Some objects- Returns:
- The passed array
-
arrayType
Returns aClass
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 arraystartIndex
- 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 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(@Nullable @org.jetbrains.annotations.Nullable double[] doubles) -
wrap
-