package com.iailab.framework.common.util.collection; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.IterUtil; import cn.hutool.core.util.ArrayUtil; import java.util.Collection; import java.util.function.Consumer; import java.util.function.Function; import static com.iailab.framework.common.util.collection.CollectionUtils.convertList; /** * Array 工具类 * * @author iailab */ public class ArrayUtils { /** * 将 object 和 newElements 合并成一个数组 * * @param object 对象 * @param newElements 数组 * @param 泛型 * @return 结果数组 */ @SafeVarargs public static Consumer[] append(Consumer object, Consumer... newElements) { if (object == null) { return newElements; } Consumer[] result = ArrayUtil.newArray(Consumer.class, 1 + newElements.length); result[0] = object; System.arraycopy(newElements, 0, result, 1, newElements.length); return result; } public static V[] toArray(Collection from, Function mapper) { return toArray(convertList(from, mapper)); } @SuppressWarnings("unchecked") public static T[] toArray(Collection from) { if (CollectionUtil.isEmpty(from)) { return (T[]) (new Object[0]); } return ArrayUtil.toArray(from, (Class) IterUtil.getElementType(from.iterator())); } public static T get(T[] array, int index) { if (null == array || index >= array.length) { return null; } return array[index]; } }