Class ArrayTools


  • public final class ArrayTools
    extends Object
    Utility class for array-related helper methods.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T[] after​(T[] arr, int index)
      Gets an array containing all elements of the designated array after (and without) the designated index.
      static <T> T[] before​(T[] arr, int index)
      Gets an array containing all elements of the designated array before (and without) the designated index.
      static boolean contains​(int[] arr, int elem)
      Gets whether the designated array contains the designated integer element.
      static <T> boolean contains​(T[] arr, T elem)
      Gets whether the designated array contains the designated element.
      static <T> boolean equalsIgnoreOrder​(T[] arr1, T[] arr2)
      Gets whether the designated arrays contain the same elements but not necessarily in the same order.
      static int[] join​(int[]... arrays)
      Creates a new int array containing all elements of the designated input arrays
      static <T> T[] join​(T[]... arrays)
      Returns a new array of the same type as all input arrays filled with their joined content.
      static <S,​T>
      T[]
      map​(Function<? super S,​? extends T> function, S[] arr, Class<T> cls)
      Applies the designated function to each element of the designated array and returns a new array.
      static <T> T[] map​(Function<? super T,​? extends T> function, T[] arr)
      Applies the designated function to each element of the designated array and returns it.
      static <T> T[] removeElement​(T[] arr, int index)
      Removes the element at the designated index from the designated array.
      static <T> T[] reverse​(T[] array)
      Reverses the entries in the designated array and returns the very same array afterwards.
      static <T> T[] toArray​(Collection<T> coll, Class<T> componentType)
      Returns the content of the given collection in a newly created array with the given component type and the size of the collection.
      static <T> T[] toArray​(T... content)
      Creates an array of the designated content.
      static <T> List<T> toList​(T[] array)
      Returns the content of the given array in a newly created (array) list.
      static Object[] toObjectArray​(Object arr)
      Converts the designated untyped array to an Object[] containing the same elements and therefore of the original type except for primitive types.
      static <T> Set<T> toSet​(T[] array)
      Gets the content of the designated array in a newly created set.
      static <S,​T>
      T[]
      transformToArray​(Collection<S> coll, Function<S,​T> transformator, Class<T> componentType)
      Gets an array containing the elements of the designated collection transformed by the designated transformer.
      static <S,​T>
      T[]
      transformToArray​(Collection<S> coll, Function<S,​T> transformator, Supplier<T> nullSupplier, Class<T> componentType)
      Gets an array containing the elements of the designated collection transformed by the designated transformer.
      static <C extends Collection<T>,​S,​T>
      C
      transformToCollection​(S[] arr, Function<Integer,​C> collCreator, Function<S,​T> transformator)
      Gets a collection containing the elements of the designated array transformed by the designated transformer.
      static <C extends Collection<T>,​S,​T>
      C
      transformToCollection​(S[] arr, Function<Integer,​C> collCreator, Function<S,​T> transformator, Supplier<T> nullSupplier)
      Gets a collection containing the elements of the designated array transformed by the designated transformer.
      static <S,​T>
      List<T>
      transformToList​(S[] arr, Function<S,​T> transformator)
      Gets a list containing the elements of the designated array transformed by the designated transformer.
      static <S,​T>
      List<T>
      transformToList​(S[] arr, Function<S,​T> transformator, Supplier<T> nullSupplier)
      Gets a list containing the elements of the designated array transformed by the designated transformer.
      static <S,​T>
      Set<T>
      transformToSet​(S[] arr, Function<S,​T> transformator)
      Gets a set containing the elements of the designated array transformed by the designated transformer.
      static <S,​T>
      Set<T>
      transformToSet​(S[] arr, Function<S,​T> transformator, Supplier<T> nullSupplier)
      Gets a set containing the elements of the designated array transformed by the designated transformer.
    • Method Detail

      • toArray

        public static <T> T[] toArray​(T... content)
        Creates an array of the designated content. This allows for easy null and singleton array behaviour.
        Note that null behaviour depends on how the method is called, while (T) null returns an array with a null element, (T[]) null returns null (and no array).
        Parameters:
        content - The content of the array to return.
        Returns:
        An array containing the designated content or null.
      • toObjectArray

        public static Object[] toObjectArray​(Object arr)
        Converts the designated untyped array to an Object[] containing the same elements and therefore of the original type except for primitive types. Primitive types will be converted to the corresponding wrapper types.
        Parameters:
        arr - An arbitrarily typed array. This may be null.
        Returns:
        An array containing the same elements (or the corresponding wrapped elements is case of primitive types) as the designated array. null will be returned in case null is provided.
        Throws:
        IllegalArgumentException - If the designated object is no array, an IllegalArgumentException will be thrown.
      • reverse

        public static <T> T[] reverse​(T[] array)
        Reverses the entries in the designated array and returns the very same array afterwards.
        Type Parameters:
        T - The type of the elements of the array to be reversed. Note that this cannot be a simple type.
        Parameters:
        array - The array to reverse the order of.
        Returns:
        The array having its order reversed. The return value can be ignored since the order is changed via side-effects on the original array.
      • join

        public static <T> T[] join​(T[]... arrays)
        Returns a new array of the same type as all input arrays filled with their joined content. Only use this method where it's not possible to use Lists instead.
        Type Parameters:
        T - component type of the arrays
        Parameters:
        arrays - the arrays to be joined
        Returns:
        new array filled with the joined content of the specified arrays
      • join

        public static int[] join​(int[]... arrays)
        Creates a new int array containing all elements of the designated input arrays
        Parameters:
        arrays - The int arrays to be joined.
        Returns:
        A new int array filled with the joined content of the designated int arrays.
      • map

        public static <T> T[] map​(Function<? super T,​? extends T> function,
                                  T[] arr)
        Applies the designated function to each element of the designated array and returns it. The array content will be changed!
        Type Parameters:
        T - The type of the array content.
        Parameters:
        function - The function which to apply to each array element.
        arr - The array to which to apply the designated function.
        Returns:
        The designated array after applying the designated function.
      • map

        public static <S,​T> T[] map​(Function<? super S,​? extends T> function,
                                          S[] arr,
                                          Class<T> cls)
        Applies the designated function to each element of the designated array and returns a new array. The array content will not be changed.
        Type Parameters:
        S - The type of the source array content.
        T - The type of the target array content.
        Parameters:
        function - The function which to apply to each array element.
        arr - The array to which to apply the designated function.
        cls - The target class.
        Returns:
        A newly created array containing the result values from the designated function.
      • toArray

        public static <T> T[] toArray​(Collection<T> coll,
                                      Class<T> componentType)
        Returns the content of the given collection in a newly created array with the given component type and the size of the collection.
        Type Parameters:
        T -
        Parameters:
        coll - the collection whose elements should be copied to a new array
        componentType - the component type of the array to be created
        Returns:
      • transformToArray

        public static <S,​T> T[] transformToArray​(Collection<S> coll,
                                                       Function<S,​T> transformator,
                                                       Class<T> componentType)
        Gets an array containing the elements of the designated collection transformed by the designated transformer. If the collection is null, null will be returned.
        Type Parameters:
        S - The type of the elements of the collection.
        T - The type of the elements in the newly created array.
        Parameters:
        coll - The collection of which the elements should be transformed and copied to a new array. null elements will not be transformed but just taken over.
        transformator - The transformator which is applied to all (non-null) elements of the designated collection.
        componentType - the component type of the array to be created,
        Returns:
        An array containing the elements of the designated collection transformed by the designated transformator or null.
      • transformToArray

        public static <S,​T> T[] transformToArray​(Collection<S> coll,
                                                       Function<S,​T> transformator,
                                                       Supplier<T> nullSupplier,
                                                       Class<T> componentType)
        Gets an array containing the elements of the designated collection transformed by the designated transformer. If the collection is null, null will be returned. All null elements in the collection will be replaced by the values from the designated supplier.
        Type Parameters:
        S - The type of the elements of the collection.
        T - The type of the elements in the newly created array.
        Parameters:
        coll - The collection of which the elements should be transformed and copied to a new array. null elements will be replaced by the designated supplier.
        transformator - The transformator which is applied to all (non-null) elements of the designated collection.
        nullSupplier - The supplier providing the object to be used for null. If this is null, null will be used as replacing value.
        componentType - the component type of the array to be created,
        Returns:
        An array containing the elements of the designated collection transformed by the designated transformator or null.
      • toList

        public static <T> List<T> toList​(T[] array)
        Returns the content of the given array in a newly created (array) list.
        Parameters:
        array - an array
        Returns:
        an array list with the same content as the given array
      • transformToList

        public static <S,​T> List<T> transformToList​(S[] arr,
                                                          Function<S,​T> transformator)
        Gets a list containing the elements of the designated array transformed by the designated transformer. If the array is null, null will be returned.
        Type Parameters:
        S - The type of the elements of the array.
        T - The type of the elements in the newly created list.
        Parameters:
        arr - The array of which the elements should be transformed and copied to a new list. null elements will not be transformed but just taken over.
        transformator - The transformator which is applied to all (non-null) elements of the designated array.
        Returns:
        A list containing the elements of the designated array transformed by the designated transformator or null.
      • transformToList

        public static <S,​T> List<T> transformToList​(S[] arr,
                                                          Function<S,​T> transformator,
                                                          Supplier<T> nullSupplier)
        Gets a list containing the elements of the designated array transformed by the designated transformer. If the array is null, null will be returned.
        Type Parameters:
        S - The type of the elements of the array.
        T - The type of the elements in the newly created list.
        Parameters:
        arr - The array of which the elements should be transformed and copied to a new list. null elements will be replaced by the designated supplier.
        transformator - The transformator which is applied to all (non-null) elements of the designated array.
        nullSupplier - The supplier providing the object to be used for null. If this is null, null will be used as replacing value.
        Returns:
        A list containing the elements of the designated array transformed by the designated transformator or null.
      • toSet

        public static <T> Set<T> toSet​(T[] array)
        Gets the content of the designated array in a newly created set. This allows for efficient comparisons, e. g. Set.contains(Object).
        Parameters:
        array - The array which to get as (Hash)Set.
        Returns:
        A (Hash)Set containing the elements of the designated array.
      • transformToSet

        public static <S,​T> Set<T> transformToSet​(S[] arr,
                                                        Function<S,​T> transformator)
        Gets a set containing the elements of the designated array transformed by the designated transformer. If the array is null, null will be returned.
        Type Parameters:
        S - The type of the elements of the array.
        T - The type of the elements in the newly created set.
        Parameters:
        arr - The array of which the elements should be transformed and copied to a new set. null elements will not be transformed but just taken over.
        transformator - The transformator which is applied to all (non-null) elements of the designated array.
        Returns:
        A set containing the elements of the designated array transformed by the designated transformator or null.
      • transformToSet

        public static <S,​T> Set<T> transformToSet​(S[] arr,
                                                        Function<S,​T> transformator,
                                                        Supplier<T> nullSupplier)
        Gets a set containing the elements of the designated array transformed by the designated transformer. If the array is null, null will be returned.
        Type Parameters:
        S - The type of the elements of the array.
        T - The type of the elements in the newly created set.
        Parameters:
        arr - The array of which the elements should be transformed and copied to a new set. null elements will be replaced by the designated supplier.
        transformator - The transformator which is applied to all (non-null) elements of the designated array.
        nullSupplier - The supplier providing the object to be used for null. If this is null, null will be used as replacing value.
        Returns:
        A set containing the elements of the designated array transformed by the designated transformator or null.
      • transformToCollection

        public static <C extends Collection<T>,​S,​T> C transformToCollection​(S[] arr,
                                                                                        Function<Integer,​C> collCreator,
                                                                                        Function<S,​T> transformator)
        Gets a collection containing the elements of the designated array transformed by the designated transformer. If the array is null, null will be returned, otherwise the designated function will be called to create the collection; the function gets the length of the designated array.
        Type Parameters:
        C - The type of collection which to create.
        S - The type of the elements of the array.
        T - The type of the elements in the newly created collection.
        Parameters:
        arr - The array of which the elements should be transformed and copied to a new collection. null elements will not be transformed but just taken over.
        collCreator - The function getting the length of the array and creating an appropriate collection.
        transformator - The transformator which is applied to all (non-null) elements of the designated array.
        Returns:
        A collection containing the elements of the designated array transformed by the designated transformator or null.
      • transformToCollection

        public static <C extends Collection<T>,​S,​T> C transformToCollection​(S[] arr,
                                                                                        Function<Integer,​C> collCreator,
                                                                                        Function<S,​T> transformator,
                                                                                        Supplier<T> nullSupplier)
        Gets a collection containing the elements of the designated array transformed by the designated transformer. If the array is null, null will be returned, otherwise the designated function will be called to create the collection; the function gets the length of the designated array.
        Type Parameters:
        C - The type of collection which to create.
        S - The type of the elements of the array.
        T - The type of the elements in the newly created collection.
        Parameters:
        arr - The array of which the elements should be transformed and copied to a new null elements will be replaced by the designated supplier.
        collCreator - The function getting the length of the array and creating an appropriate collection.
        transformator - The transformator which is applied to all (non-null) elements of the designated array.
        nullSupplier - The supplier providing the object to be used for null. If this is null, null will be used as replacing value.
        Returns:
        A collection containing the elements of the designated array transformed by the designated transformator or null.
      • contains

        public static boolean contains​(int[] arr,
                                       int elem)
        Gets whether the designated array contains the designated integer element.
        Note that this simply iterates the array so use it carefully to avoid runtime complexity. For instance using it within an iteration multiplies runtime complexity.
        Parameters:
        arr - The array which to check for whether it contains the designated integer element.
        elem - The integer element which to check for whether it is part of the designated array.
        Returns:
        Whether the designated integer element is part of the designated array.
      • contains

        public static <T> boolean contains​(T[] arr,
                                           T elem)
        Gets whether the designated array contains the designated element. The element can be null.
        Note that this simply iterates the array so use it carefully to avoid runtime complexity. For instance using it within an iteration multiplies runtime complexity.
        Parameters:
        arr - The array which to check for whether it contains the designated element. This may be null (which obviously contains nothing).
        elem - The element which to check for whether it is part of the designated array. This can be null.
        Returns:
        Whether the designated element is part of the designated array.
      • equalsIgnoreOrder

        public static <T> boolean equalsIgnoreOrder​(T[] arr1,
                                                    T[] arr2)
        Gets whether the designated arrays contain the same elements but not necessarily in the same order.
        Type Parameters:
        T - The type of the array elements.
        Parameters:
        arr1 - The first array of which to compare the content without order. This must not be null.
        arr2 - The second array of which to compare the content without order. This must not be null.
        Returns:
        Whether the designated arrays contain the same elements but not necessarily in the same order.
      • removeElement

        public static <T> T[] removeElement​(T[] arr,
                                            int index)
        Removes the element at the designated index from the designated array. If the designated index is outside the designated array, the array will be returned without changes.
        Parameters:
        arr - The array from which to remove an element.
        index - The index of the element which to remove.
        Returns:
        The designated array without the element at the designated index.
      • before

        public static <T> T[] before​(T[] arr,
                                     int index)
        Gets an array containing all elements of the designated array before (and without) the designated index. If the designated index is <=0, an empty array will be returned, if the designated index is bigger than the designated array, the array will be returned without changes.
        Parameters:
        arr - The array from which to remove elements.
        index - The index of the first element that is not part of the returned array.
        Returns:
        The designated array without the elements at and after the designated index.
      • after

        public static <T> T[] after​(T[] arr,
                                    int index)
        Gets an array containing all elements of the designated array after (and without) the designated index. If the designated index is >=arr.length, an empty array will be returned, if the designated index is <=0, the array will be returned without changes.
        Parameters:
        arr - The array from which to remove elements.
        index - The index of the last element that is not part of the returned array.
        Returns:
        The designated array without the elements before and at the designated index.