com.gitee.feizns.ArrayUtils Maven / Gradle / Ivy
package com.gitee.feizns;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* 数组
* @author feizns
* @since 2019/5/13
*/
public abstract class ArrayUtils {
/**
* 查找第一个元素的下标
* @param test
* @param array
* @return
*/
public static int findIndex(Predicate test, T... array) {
if ( array != null ) {
for (int i = 0; i < array.length; i++) {
if ( test.test(array[i]) )
return i;
}
}
return -1;
}
/**
* @see ArrayUtils#findIndex(java.util.function.Predicate, java.lang.Object[])
* @param test
* @param array
* @return
*/
public static final int indexOf(Predicate test, T... array) {
return findIndex(test, array);
}
/**
* 查找第一个元素
* @param test
* @param array
* @return
*/
public static T findFirst(Predicate test, T... array) {
int index = findIndex(test, array);
return index != -1 ? array[index] : null;
}
/**
* 将数组包装成iterator
* @param ele
* @param
* @return
*/
public static Iterator iterator(T... ele) {
return ele != null ? Arrays.stream(ele).iterator() : null;
}
/**
* 将数组包装成iterable
* @param ele
* @param
* @return
*/
public static Iterable iterable(T... ele) {
return () -> iterator(ele);
}
/**
* 数组是否为空
* @param ele
* @param
* @return
*/
public static final boolean isEmpty(T... ele) {
return ele == null || ele.length == 0;
}
/**
* 遍历一个未知的数组
* @param array
* @param consumer
*/
public static final void forEach(Object array, Consumer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy