ArrayUtil

Introduction

Array tool methods in the 2.x version existed in CollectionUtil, and in the 3.x version (including 4.x version), they were split out as ArrayUtil. The array tool class is mainly aimed at encapsulating solutions for both primitive type arrays and generic type arrays.

The array tool class mainly solves the problem of inconsistent usage methods between object arrays (including wrapper type arrays) and primitive type arrays.

Methods

Judging Emptiness

Similar to judging emptiness of strings, the criteria for judging emptiness of arrays is null or the length of the array being 0. ArrayUtil encapsulates methods for judging emptiness and non-emptiness of both primitive type and generic type arrays:

  1. Judging emptiness
int[] a = {};
int[] b = null;
ArrayUtil.isEmpty(a);
ArrayUtil.isEmpty(b);
  1. Judging non-emptiness
int[] a = {1,2};
ArrayUtil.isNotEmpty(a);

Creating Generic Arrays

Array.newInstance does not support generic return values, so this method is encapsulated to support generic return values with ArrayUtil.newArray(Class<T>, int):

String[] newArray = ArrayUtil.newArray(String.class, 3);

Resizing

Use the ArrayUtil.resize method to create a new array with a resized size.

Merging Arrays

ArrayUtil.addAllmethod uses variable arguments to merge multiple generic arrays into one array.

Cloning

Array itself supports the clone method, so ArrayUtil.clone(T[]) is called when the array type is certain, and ArrayUtil.clone(T) is called when the array type is uncertain. The two overloaded methods have differences in implementation, but cannot be perceived during use.

  1. Generic array calling native clone
Integer[] b = {1,2,3};
Integer[] cloneB = ArrayUtil.clone(b);
Assert.assertArrayEquals(b, cloneB);
  1. Non-generic array (primitive type array) calling the second overloaded method
int[] a = {1,2,3};
int[] clone = ArrayUtil.clone(a);
Assert.assertArrayEquals(a, clone);

Generating an Ordered List

ArrayUtil.range method has three overloaded methods, which can be used together to support ordered arrays with steps or steps of 1. This method is similar to the list generator syntax sugar in Python.

Splitting an Array

ArrayUtil.split method is used to split a byte array into equal parts, often used for message splitting.

Filtering

ArrayUtil.filter method is used to filter elements in an existing array, which only operates on generic arrays and is not provided for primitive type arrays. The Filter interface is used to return a boolean value to determine whether to retain it.

Filter an array to only keep even numbers:

Integer[] a = {1,2,3,4,5,6};
// [2,4,6]
Integer[] filter = ArrayUtil.filter(a, (Editor<Integer>) t -> (t % 2 == 0) ? t : null);

Edit an existing array to obtain edited values:

Integer[] a = {1, 2, 3, 4, 5, 6};
// [1, 20, 3, 40, 5, 60]
Integer[] filter = ArrayUtil.filter(a, (Editor<Integer>) t -> (t % 2 == 0) ? t * 10 : t);

zip

The ArrayUtil.zip method takes two arrays as parameters, where the first array is the key and the second array is the value at the corresponding position. This method is similar to the zip() function in Python.

String[] keys = {"a", "b", "c"};
Integer[] values = {1,2,3};
Map<String, Integer> map = ArrayUtil.zip(keys, values, true);

//{a=1, b=2, c=3}

Contains Element

The ArrayUtil.contains method is only for generic arrays and checks if a specific element is present in the array.

Wrapping and Unwrapping

In Java, automatic wrapping and unwrapping are achieved for primitive types and their corresponding wrapper types, but the same cannot be said for arrays. Therefore, the ArrayUtil.wrap and ArrayUtil.unwrap methods are used to convert between primitive type arrays and wrapper type arrays.

Determining if an Object is an Array

The ArrayUtil.isArray method encapsulates obj.getClass().isArray().

Converting to a String

  1. ArrayUtil.toString - When converting a primitive type array to a string, it cannot be displayed normally. This method is encapsulated to perfectly support the conversion of both primitive type arrays and wrapper type arrays to strings.
  2. ArrayUtil.join - This method uses a separator to convert an array to a string. For example, if the array [1,2,3,4] is converted to a string with a separator of “-”, the result will be “1-2-3-4”. The join method supports both generic arrays and primitive type arrays.

toArray

The ArrayUtil.toArray method provides convenience for converting ByteBuffer to an array.