Package com.dianxin.tori.base.utils
Class ArrayUtils
java.lang.Object
com.dianxin.tori.base.utils.ArrayUtils
Utility class providing helper methods for standard array conversions and element checks.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> booleancontains(T[] array, T element) Checks if a specific element exists within the provided array.static <T> T[]toArray(@Nullable Collection<T> collection, @NotNull IntFunction<T[]> generator) Converts aCollectioninto an array using the provided generator function.static <T> @NotNull List<T> toList(T[] array) Converts an array into a mutableList.static <T> @NotNull Set<T> toSet(T[] array) Converts an array into a mutableSet.
-
Method Details
-
toList
Converts an array into a mutableList.- Type Parameters:
T- the element type- Parameters:
array- the source array, can benull- Returns:
- a mutable
ArrayListcontaining the array elements, or an empty list if array isnullor empty
-
toSet
Converts an array into a mutableSet.- Type Parameters:
T- the element type- Parameters:
array- the source array, can benull- Returns:
- a mutable
HashSetcontaining unique array elements, or an empty set if array isnullor empty
-
toArray
@NotNull public static <T> T[] toArray(@Nullable @Nullable Collection<T> collection, @NotNull @NotNull IntFunction<T[]> generator) Converts aCollectioninto an array using the provided generator function.- Type Parameters:
T- the element type of the collection and the resulting array- Parameters:
collection- the source collection to be converted, can benullgenerator- the array constructor reference used to allocate the array of the correct type (e.g.,String[]::new)- Returns:
- an array containing all the elements in the collection,
or an empty array of type
Tif the collection isnullor empty - Throws:
IllegalArgumentException- if thegeneratorisnulland the collection is not null or empty
-
contains
public static <T> boolean contains(@Nullable T[] array, @Nullable T element) Checks if a specific element exists within the provided array.- Type Parameters:
T- the element type- Parameters:
array- the array to inspect, can benullelement- the element to find, can benull- Returns:
trueif the element is found, otherwisefalse
-