Class ArrayTools


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

      Fields 
      Modifier and Type Field Description
      static String ELEMENT_SEPARATOR
      The string used for separating array elements when transformed to a string.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T[] addElement​(T[] arr, T elem)
      Adds the designated element at the end of the designated array
      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 <T> T[] fromString​(Class<T> cls, Function<String,​? extends T> conv, String separator, String values)
      Converts the designated string that has elements separated by the designated separator string to an array of the corresponding object instances.
      static <T> T[] fromString​(Class<T> cls, Function<String,​T> conv, String values)
      Converts the designated string that has elements separated by ELEMENT_SEPARATOR to an array of the corresponding object instances.
      static int getIndex​(int[] arr, int elem)
      Gets the index of the designated integer element in the designated array or -1 if the array does not contain the integer element.
      static <T> int getIndex​(T[] arr, T elem)
      Gets the index of the designated element in the designated array or -1 if the array does not contain the element.
      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 (that is a new array without the element is returned).
      static <T> T[] repeat​(int size, T elem)
      Creates a new array of the designated size repeating the designated element.
      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 <T> String toString​(Function<T,​String> conv, String separator, T... arr)
      Gets the string representation of the designated array (elements) using the designated function to create the string representations and the designated separator to distinguish the elements in the string.
      static <T> String toString​(Function<T,​String> conv, T... arr)
      Gets the string representation of the designated array (elements) using the designated function to create the string representations.
      static <T> String toString​(T... arr)
      Gets the string representation of the designated array (elements).
      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.
    • Field Detail

      • ELEMENT_SEPARATOR

        public static final String ELEMENT_SEPARATOR
        The string used for separating array elements when transformed to a string.
        See Also:
        Constant Field Values
    • Method Detail

      • toArray

        @SafeVarargs
        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.
      • repeat

        public static <T> T[] repeat​(int size,
                                     T elem)
        Creates a new array of the designated size repeating the designated element.
        Type Parameters:
        T - The type of the element.
        Parameters:
        size - The size of the array to create.
        elem - The element which to repeat at each array position.
        Returns:
        An array of the designated size containing the designated element at each position.
      • 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:
        An array containing the elements of the designated collection or null if the designated collection is null.
      • 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.
      • getIndex

        public static int getIndex​(int[] arr,
                                   int elem)
        Gets the index of the designated integer element in the designated array or -1 if the array does not contain the 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 of which to get the index of the designated integer element.
        elem - The integer element of which to get the index within the designated array.
        Returns:
        The index of the designated integer element within the designated array or -1.
      • 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.
      • getIndex

        public static <T> int getIndex​(T[] arr,
                                       T elem)
        Gets the index of the designated element in the designated array or -1 if the array does not contain the 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 of which to get the index of the designated element.
        elem - The element of which to get the index within the designated array.
        Returns:
        The index of the designated element within the designated array or -1.
      • 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.
      • addElement

        public static <T> T[] addElement​(T[] arr,
                                         T elem)
        Adds the designated element at the end of the designated array
        Parameters:
        arr - The array to which to add an element.
        elem - The element which to add.
        Returns:
        A new array with the same content as the designated array with the designated element additionally at the last index.
      • removeElement

        public static <T> T[] removeElement​(T[] arr,
                                            int index)
        Removes the element at the designated index from the designated array (that is a new array without the element is returned). 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:
        A new array with the same content as the designated array without the element at the designated index or the designated array unchanged.
      • 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.
      • toString

        public static <T> String toString​(T... arr)
        Gets the string representation of the designated array (elements). The elements will be separated by ELEMENT_SEPARATOR. Make sure that the separator is not part of the string representation of an element.
        Type Parameters:
        T - The type of the elements converted to string.
        Parameters:
        arr - The array of elements converted to string.
        Returns:
        A string representation of the designated array (elements).
      • toString

        public static <T> String toString​(Function<T,​String> conv,
                                          T... arr)
        Gets the string representation of the designated array (elements) using the designated function to create the string representations. The strings will be separated by ELEMENT_SEPARATOR. Make sure that the separator is not part of the string representation of an element.
        Type Parameters:
        T - The type of the elements converted to string.
        Parameters:
        conv - The function converting an element of the designated array to string.
        arr - The array of elements converted to string.
        Returns:
        A string representation of the designated array (elements) using the designated function for creating representations of the elements.
      • toString

        public static <T> String toString​(Function<T,​String> conv,
                                          String separator,
                                          T... arr)
        Gets the string representation of the designated array (elements) using the designated function to create the string representations and the designated separator to distinguish the elements in the string. Make sure that the separator is not part of the string representation of an element.
        Type Parameters:
        T - The type of the elements converted to string.
        Parameters:
        conv - The function converting an element of the designated array to string.
        separator - The string used to separate the elements in the created string.
        arr - The array of elements converted to string.
        Returns:
        A string representation of the designated array (elements) using the designated function for creating representations of the elements and separating them with the designated separator.
      • fromString

        public static <T> T[] fromString​(Class<T> cls,
                                         Function<String,​T> conv,
                                         String values)
        Converts the designated string that has elements separated by ELEMENT_SEPARATOR to an array of the corresponding object instances. The designated function will be used for converting an element from string to the appropriate type.
        Type Parameters:
        T - The type of the elements represented by the designated string.
        Parameters:
        cls - The element type.
        conv - The function converting an element from string to an instance of the appropriate type.
        values - The string representation to convert to an array of object instances.
        Returns:
        An array of the object instances represented by the designated string.
      • fromString

        public static <T> T[] fromString​(Class<T> cls,
                                         Function<String,​? extends T> conv,
                                         String separator,
                                         String values)
        Converts the designated string that has elements separated by the designated separator string to an array of the corresponding object instances. The designated function will be used for converting an element from string to the appropriate type.
        Type Parameters:
        T - The type of the elements represented by the designated string.
        Parameters:
        cls - The element type.
        conv - The function converting an element from string to an instance of the appropriate type.
        separator - The string used to separate the elements in the designated string.
        values - The string representation to convert to an array of object instances.
        Returns:
        An array of the object instances represented by the designated string or null if the designated string has no value.