forked from plusone/plusone-commons
添加注释
This commit is contained in:
@@ -30,68 +30,147 @@ public class ArrayTools {
|
||||
|
||||
// isNullOrEmpty
|
||||
|
||||
/**
|
||||
* 检查给定数组是否为空
|
||||
*
|
||||
* @param arr 待检查的数组,可以为 {@code null}
|
||||
* @param <T> 数组中元素的类型
|
||||
* @return 如果数组为 {@code null} 或长度为 0,则返回 {@code true};否则返回 {@code false}
|
||||
*/
|
||||
public static <T> boolean isNullOrEmpty(@Nullable T[] arr) {
|
||||
return arr == null || arr.length == 0;
|
||||
}
|
||||
|
||||
// isNullOrEmpty - float
|
||||
/**
|
||||
* 检查给定数组是否为空
|
||||
*
|
||||
* @param arr 待检查的数组,可以为 {@code null}
|
||||
* @return 如果数组为 {@code null} 或长度为 0,则返回 {@code true};否则返回 {@code false}
|
||||
*/
|
||||
public static boolean isNullOrEmpty(@Nullable float[] arr) {
|
||||
return arr == null || arr.length == 0;
|
||||
}
|
||||
|
||||
// isNullOrEmpty - double
|
||||
/**
|
||||
* 检查给定数组是否为空
|
||||
*
|
||||
* @param arr 待检查的数组,可以为 {@code null}
|
||||
* @return 如果数组为 {@code null} 或长度为 0,则返回 {@code true};否则返回 {@code false}
|
||||
*/
|
||||
public static boolean isNullOrEmpty(@Nullable double[] arr) {
|
||||
return arr == null || arr.length == 0;
|
||||
}
|
||||
|
||||
// isNullOrEmpty - byte
|
||||
/**
|
||||
* 检查给定数组是否为空
|
||||
*
|
||||
* @param arr 待检查的数组,可以为 {@code null}
|
||||
* @return 如果数组为 {@code null} 或长度为 0,则返回 {@code true};否则返回 {@code false}
|
||||
*/
|
||||
public static boolean isNullOrEmpty(@Nullable byte[] arr) {
|
||||
return arr == null || arr.length == 0;
|
||||
}
|
||||
|
||||
// isNullOrEmpty - long
|
||||
/**
|
||||
* 检查给定数组是否为空
|
||||
*
|
||||
* @param arr 待检查的数组,可以为 {@code null}
|
||||
* @return 如果数组为 {@code null} 或长度为 0,则返回 {@code true};否则返回 {@code false}
|
||||
*/
|
||||
public static boolean isNullOrEmpty(@Nullable long[] arr) {
|
||||
return arr == null || arr.length == 0;
|
||||
}
|
||||
|
||||
// isNullOrEmpty - int
|
||||
/**
|
||||
* 检查给定数组是否为空
|
||||
*
|
||||
* @param arr 待检查的数组,可以为 {@code null}
|
||||
* @return 如果数组为 {@code null} 或长度为 0,则返回 {@code true};否则返回 {@code false}
|
||||
*/
|
||||
public static boolean isNullOrEmpty(@Nullable int[] arr) {
|
||||
return arr == null || arr.length == 0;
|
||||
}
|
||||
|
||||
// isNotEmpty
|
||||
|
||||
/**
|
||||
* 检查给定数组是否不为空
|
||||
*
|
||||
* @param arr 待检查的数组,可以为 {@code null}
|
||||
* @param <T> 数组中元素的类型
|
||||
* @return 如果数组不为 {@code null} 且长度大于 0,则返回 {@code true};否则返回 {@code false}
|
||||
*/
|
||||
public static <T> boolean isNotEmpty(@Nullable T[] arr) {
|
||||
return arr != null && arr.length > 0;
|
||||
}
|
||||
|
||||
// isNotEmpty - float
|
||||
/**
|
||||
* 检查给定数组是否不为空
|
||||
*
|
||||
* @param arr 待检查的数组,可以为 {@code null}
|
||||
* @return 如果数组不为 {@code null} 且长度大于 0,则返回 {@code true};否则返回 {@code false}
|
||||
*/
|
||||
public static boolean isNotEmpty(@Nullable float[] arr) {
|
||||
return arr != null && arr.length > 0;
|
||||
}
|
||||
|
||||
// isNotEmpty - double
|
||||
/**
|
||||
* 检查给定数组是否不为空
|
||||
*
|
||||
* @param arr 待检查的数组,可以为 {@code null}
|
||||
* @return 如果数组不为 {@code null} 且长度大于 0,则返回 {@code true};否则返回 {@code false}
|
||||
*/
|
||||
public static boolean isNotEmpty(@Nullable double[] arr) {
|
||||
return arr != null && arr.length > 0;
|
||||
}
|
||||
|
||||
// isNotEmpty - byte
|
||||
/**
|
||||
* 检查给定数组是否不为空
|
||||
*
|
||||
* @param arr 待检查的数组,可以为 {@code null}
|
||||
* @return 如果数组不为 {@code null} 且长度大于 0,则返回 {@code true};否则返回 {@code false}
|
||||
*/
|
||||
public static boolean isNotEmpty(@Nullable byte[] arr) {
|
||||
return arr != null && arr.length > 0;
|
||||
}
|
||||
|
||||
// isNotEmpty - long
|
||||
/**
|
||||
* 检查给定数组是否不为空
|
||||
*
|
||||
* @param arr 待检查的数组,可以为 {@code null}
|
||||
* @return 如果数组不为 {@code null} 且长度大于 0,则返回 {@code true};否则返回 {@code false}
|
||||
*/
|
||||
public static boolean isNotEmpty(@Nullable long[] arr) {
|
||||
return arr != null && arr.length > 0;
|
||||
}
|
||||
|
||||
// isNotEmpty - int
|
||||
/**
|
||||
* 检查给定数组是否不为空
|
||||
*
|
||||
* @param arr 待检查的数组,可以为 {@code null}
|
||||
* @return 如果数组不为 {@code null} 且长度大于 0,则返回 {@code true};否则返回 {@code false}
|
||||
*/
|
||||
public static boolean isNotEmpty(@Nullable int[] arr) {
|
||||
return arr != null && arr.length > 0;
|
||||
}
|
||||
|
||||
// concat
|
||||
|
||||
/**
|
||||
* 拼接多个数组
|
||||
*
|
||||
* @param arrays 数组集合,可以为 {@code null}
|
||||
* @return 拼接后的数组
|
||||
*/
|
||||
public static float[] concatFloatArray(@Nullable Collection<float[]> arrays) {
|
||||
if (arrays == null || arrays.isEmpty()) {
|
||||
return new float[0];
|
||||
@@ -106,6 +185,12 @@ public class ArrayTools {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼接多个数组
|
||||
*
|
||||
* @param arrays 数组集合,可以为 {@code null}
|
||||
* @return 拼接后的数组
|
||||
*/
|
||||
public static double[] concatDoubleArray(@Nullable Collection<double[]> arrays) {
|
||||
if (arrays == null || arrays.isEmpty()) {
|
||||
return new double[0];
|
||||
@@ -120,6 +205,12 @@ public class ArrayTools {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼接多个数组
|
||||
*
|
||||
* @param arrays 数组集合,可以为 {@code null}
|
||||
* @return 拼接后的数组
|
||||
*/
|
||||
public static byte[] concatByteArray(@Nullable Collection<byte[]> arrays) {
|
||||
if (arrays == null || arrays.isEmpty()) {
|
||||
return new byte[0];
|
||||
@@ -134,6 +225,12 @@ public class ArrayTools {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼接多个数组
|
||||
*
|
||||
* @param arrays 数组集合,可以为 {@code null}
|
||||
* @return 拼接后的数组
|
||||
*/
|
||||
public static long[] concatLongArray(@Nullable Collection<long[]> arrays) {
|
||||
if (arrays == null || arrays.isEmpty()) {
|
||||
return new long[0];
|
||||
@@ -148,6 +245,12 @@ public class ArrayTools {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼接多个数组
|
||||
*
|
||||
* @param arrays 数组集合,可以为 {@code null}
|
||||
* @return 拼接后的数组
|
||||
*/
|
||||
public static int[] concatIntArray(@Nullable Collection<int[]> arrays) {
|
||||
if (arrays == null || arrays.isEmpty()) {
|
||||
return new int[0];
|
||||
@@ -162,15 +265,28 @@ public class ArrayTools {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将集合中的数组连接为一个列表
|
||||
*
|
||||
* @param arrays 可能包含多个数组的集合,这些数组中的元素将被连接到一个列表中
|
||||
* @param <T> 泛型参数,表示数组的元素类型
|
||||
* @return 返回连接后的列表,如果输入的集合为空或包含空数组,则返回空列表
|
||||
*/
|
||||
public static <T> List<T> concatToList(@Nullable Collection<T[]> arrays) {
|
||||
// 如果输入的集合是否为空,则直接返回一个空列表
|
||||
if (arrays == null || arrays.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
// 计算所有数组的总长度,用于初始化列表的容量
|
||||
final int length = arrays.stream().mapToInt(a -> a.length).sum();
|
||||
final List<T> result = new ArrayList<>(length);
|
||||
|
||||
for (T[] arr : arrays) {
|
||||
Collections.addAll(result, arr);
|
||||
}
|
||||
|
||||
// 返回连接后的列表
|
||||
return result;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user