开源项目地址: github开源链接
使用感觉就是,麻雀虽小五脏俱全..
本代码非jdk1.8不支持,首先先定义几个functionInterface:
@FunctionalInterface
public interface BeanOperator<T> {
/**
* 适用于普通对象
* @param t
*/
public boolean operator(T t);
}
@FunctionalInterface
public interface ListOperator<T> {
/**
* 适用于集合等有下标的操作
* @param t
* @param index
*/
public boolean operator(T t,int index);
}
@FunctionalInterface
public interface MapOperator<K,V> {
/**
* 适用于Map等键值对的操作
* @param t
* @param key
*/
public boolean operator(K key,V value);
}
接下来便是工具类
package com.cykj.base.common.util;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import org.springframework.util.ConcurrentReferenceHashMap;
import com.cykj.base.common.model.lambda.BeanOperator;
import com.cykj.base.common.model.lambda.FieldOperator;
import com.cykj.base.common.model.lambda.ListOperator;
import com.cykj.base.common.model.lambda.MapOperator;
/**
* 数组工具类
*
* @author LeiYong
*/
public class ArrayUtil {
private static final Map<Class<?>, Field[]> declaredFieldsCache = new ConcurrentReferenceHashMap<Class<?>, Field[]>(
256);
private static final Field[] NO_FIELDS = {};
/**
* 对list执行排序
*
* @param list
* @param asc
* 是否正序排序
* @return
*/
public static <T extends Comparable<T>> List<T> sortList(List<T> list, boolean asc) {
if (list == null) {
return list;
}
list.sort((T a, T b) -> {
if (asc) {
return a.compareTo(b);
} else {
return b.compareTo(a);
}
});
return list;
}
/**
* 对list中数据执行过滤
*
* @param list
* @param predicate
* 过滤接口
* @return
*/
@SuppressWarnings("unchecked")
public static <T> List<T> filter(List<T> list, Predicate<T> predicate) {
return (List<T>) Arrays.asList(list.stream().filter(predicate).toArray());
}
/**
* 遍历List 请勿做删除操作,需执行删除请调用list原生foreach((T t)->{list.remove(t);})
*
* @param list
* @param operator
* @return 仅当operate返回false时,停止遍历并返回false
*/
public static <T> boolean foreach(List<T> list, ListOperator<T> operator) {
if (list == null) {
return true;
}
for (int i = 0; i < list.size(); i++) {
boolean res = operator.operator(list.get(i), i);
if (!res) {
return res;
}
}
return true;
}
/**
* 遍历Array 请勿做删除操作,需执行删除请调用list原生foreach((T t)->{list.remove(t);})
*
* @param array
* @param operator
* @return 仅当operate返回false时,停止遍历并返回false
*/
public static <T> boolean foreach(T[] array, ListOperator<T> operator) {
if (array == null) {
return true;
}
for (int i = 0; i < array.length; i++) {
boolean res = operator.operator(array[i], i);
if (!res) {
return res;
}
}
return true;
}
/**
* 遍历Set
*
* @param list
* @param operator
* @return 仅当operate返回false时,停止遍历并返回false
*/
public static <T> boolean foreach(Set<T> set, BeanOperator<T> operator) {
if (set == null) {
return true;
}
for (T k : set) {
boolean res = operator.operator(k);
if (!res) {
return res;
}
}
return true;
}
/**
* 遍历Map
*
* @param list
* @param operator
* @return 仅当operate返回false时,停止遍历并返回false
*/
public static <K, V> boolean foreach(Map<K, V> map, MapOperator<K, V> operator) {
if (map == null) {
return false;
}
return foreach(map.keySet(), (k) -> {
return operator.operator(k, map.get(k));
});
}
/**
* 遍历Bean
* 此处参考spring遍历bean方法,采用jdk1.8特性重写
* @param obj
* @param operator
* @param fieldFilter 属性过滤器,不过滤为null
* @return 仅当operate返回false时,停止遍历并返回false
*/
public static<K,V> boolean foreach(Object obj,boolean inSuper,FieldOperator operator){
if (obj==null) {
return true;
}
Class<?> targetClass = obj.getClass();
do {
Field[] fields = getDeclaredFields(targetClass);
for (Field field : fields) {
boolean res = operator.operator(field, ReflectionUtil.getFieldAndSetAccessible(field, obj));
if (!res) {
return res;
}
}
}
while (inSuper&&(targetClass = targetClass.getSuperclass()) != null && targetClass != Object.class);
return true;
}
/**
* 获取Bean属性,并缓存
* @param clazz
* @return
*/
private static Field[] getDeclaredFields(Class<?> clazz) {
Field[] result = declaredFieldsCache.get(clazz);
if (result == null) {
result = clazz.getDeclaredFields();
declaredFieldsCache.put(clazz, (result.length == 0 ? NO_FIELDS : result));
}
return result;
}
}
最后写一个junit做一个简单测试,代码如下:
package com.cykj.common.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.cykj.base.common.util.ArrayUtil;
public class ArrayUtilTest {
/**
* Logger for this class
*/
private static final Logger logger = LoggerFactory.getLogger(ArrayUtilTest.class);
List<Integer> list;
@Before
public void testBefore(){
if (logger.isDebugEnabled()) {
logger.debug("testBefore() - start"); //$NON-NLS-1$
}
list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
list.add((int)(Math.random()*100));
}
if (logger.isDebugEnabled()) {
logger.debug("testBefore() - end"); //$NON-NLS-1$
}
}
@Test
public void testSortList() {
if (logger.isDebugEnabled()) {
logger.debug("testSortList() - start"); //$NON-NLS-1$
}
System.out.println("this is sort method");
System.out.println(ArrayUtil.sortList(list, true));
if (logger.isDebugEnabled()) {
logger.debug("testSortList() - end"); //$NON-NLS-1$
}
}
@Test
public void testFileter() {
if (logger.isDebugEnabled()) {
logger.debug("testFileter() - start"); //$NON-NLS-1$
}
System.out.println("this is filter method");
System.out.println(ArrayUtil.filter(list,(t) -> {return t > 50;}));
if (logger.isDebugEnabled()) {
logger.debug("testFileter() - end"); //$NON-NLS-1$
}
}
@Test
public void testForeachList () {
if (logger.isDebugEnabled()) {
logger.debug("testFileter() - start"); //$NON-NLS-1$
}
System.out.println("this is foreach list method");
//一旦大于95停止遍历
boolean res = ArrayUtil.foreach(list,(t,i) -> {System.out.println(i+"--"+t);return t<95;});
System.out.println("是否有大于95的数字:"+!res);
if (logger.isDebugEnabled()) {
logger.debug("testFileter() - end"); //$NON-NLS-1$
}
}
@Test
public void testForeachMap () {
if (logger.isDebugEnabled()) {
logger.debug("testFileter() - start"); //$NON-NLS-1$
}
Map<String, Object> map = new HashMap<>();
map.put("ss", "tt");
map.put("ii", 193);
map.put("ll", list);
System.out.println("this is foreach list method");
ArrayUtil.foreach(map,(k,v) -> {System.out.println(k+"::"+v);return true;});
if (logger.isDebugEnabled()) {
logger.debug("testFileter() - end"); //$NON-NLS-1$
}
}
@Test
public void testForeachSet () {
if (logger.isDebugEnabled()) {
logger.debug("testFileter() - start"); //$NON-NLS-1$
}
Set<Object> ss = new HashSet<>();
ss.add("1");
ss.add("3");
ss.add(list);
System.out.println("this is foreach list method");
ArrayUtil.foreach(ss,(s) -> {System.out.println(s);return true;});
if (logger.isDebugEnabled()) {
logger.debug("testFileter() - end"); //$NON-NLS-1$
}
}
}