好用的集合操作工具类,直接导入到utils包下
具体应用:
getSet
List zbIds =MyCollectionUtil.getList(list, “id”);
Map<String, List> ListMap =MyCollectionUtil.aggregate(MxList, “zbid”);
/**
* 功能: 集合工具类
* 从fcgoods 应用中拷贝
* @author yanguofu
* @version 1.0
*/
public class MyCollectionUtil {
/**
* 功能: 遍历list,根据function业务逻辑进行聚合分组,返回Map key=function处理的值 value=List<T>
* @author yanguofu
* @param <T> 泛型对象
* @param <FiledType> 字段对应的数据类型
* @param list 列表结合
* @param function 功能函数
* @return Map<FiledType,List<T>> FiledType 字段类型
*/
public static <T, FiledType> Map<FiledType, List<T>> aggregate(List<T> list, Function<T, FiledType> function) {
if (CollectionUtils.isEmpty(list)) {
return new HashMap<FiledType, List<T>>(MagicNumberConstant.INT16);
}
Map<FiledType, List<T>> map = new HashMap<FiledType, List<T>>(MagicNumberConstant.INT16);
List<T> tempList = null;
for (T t : list) {
FiledType fieldValue;
try {
fieldValue = function.apply(t);
tempList = map.get(fieldValue);
if (null == tempList) {
tempList = new ArrayList<T>();
tempList.add(t);
map.put(fieldValue, tempList);
} else {
tempList.add(t);
}
} catch (Exception e) {
}
}
return map;
}
/**
* 功能: 遍历list,根据fieldName属性进行聚合分组,返回Map key=fieldName属性值 value=List<T>
* @author yanguofu
* @param <T> 泛型对象
* @param <FiledType> 字段对应的数据类型
* @param list 列表集合
* @param fieldName 属性名称
* @return Map<FiledType,List<T>> Map集合结果
*/
public static <T, FiledType> Map<FiledType, List<T>> aggregate(List<T> list, String fieldName) {
if (CollectionUtils.isEmpty(list)) {
return new HashMap<FiledType, List<T>>(MagicNumberConstant.INT16);
}
Map<FiledType, List<T>> map = new HashMap<FiledType, List<T>>(MagicNumberConstant.INT16);
List<T> tempList = null;
for (T t : list) {
FiledType fieldValue;
try {
fieldValue = MyCollectionUtil.getProperty(t, fieldName);
tempList = map.get(fieldValue);
if (null == tempList) {
tempList = new ArrayList<T>();
tempList.add(t);
map.put(fieldValue, tempList);
} else {
tempList.add(t);
}
} catch (Exception e) {
}
}
return map;
}
/**
* 从list提取字段根据function业务逻辑,生成一个List集合,并返回
* @param <T> 泛型对象
* @param <FiledType> 字段对应的数据类型
* @param list 列表集合
* @param fieldName 属性名称
* @return List<FiledType> 属性值集合
*/
public static <T, FiledType> List<FiledType> getList(List<T> list, String fieldName) {
if (CollectionUtils.isEmpty(list)) {
return new ArrayList<>();
}
List<FiledType> fieldList = new ArrayList<>();
for (T t : list) {
FiledType fieldValue;
try {
fieldValue = MyCollectionUtil.getProperty(t, fieldName);
if (null != fieldValue) {
fieldList.add(fieldValue);
}
} catch (Exception e) {
}
}
return fieldList;
}
/**
* 遍历list,根据fieldName属性进行聚合分组,每个分组只有一个对象,返回Map key=fieldName属性值 value= T
* @param <T> 泛型对象
* @param <FiledType> 字段对应的数据类型
* @param list 列表集合
* @param fieldName 属性名称
* @return Map<FiledType, T> 属性值集合
*/
public static <T, FiledType> Map<FiledType, T> aggregateForOne(List<T> list, String fieldName) {
if (CollectionUtils.isEmpty(list)) {
return new HashMap<>(MagicNumberConstant.INT16);
}
Map<FiledType, T> map = new HashMap<FiledType, T>(MagicNumberConstant.INT16);
T tempObject = null;
for (T t : list) {
FiledType fieldValue;
try {
fieldValue = MyCollectionUtil.getProperty(t, fieldName);
tempObject = map.get(fieldValue);
if (tempObject != null) {
map.put(fieldValue, tempObject);
} else {
map.put(fieldValue, t);
}
} catch (Exception e) {
}
}
return map;
}
/**
* 功能: 获取对象属性值
* @author yanguofu
* @param <T> 泛型对象
* @param beanObject 对象
* @param fieldName 属性名称
* @return T 对象类型
*/
public static <T> T getProperty(Object beanObject, String fieldName) {
T fieldValue = null;
try {
fieldValue = (T) BeanUtilsBean.getInstance().getPropertyUtils().getNestedProperty(beanObject, fieldName);
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
return fieldValue;
}
/**
* 从list提取字段fieldName字段中的值生成一个Set集合,并返回
*
* @param list
* @param fieldName
* @return Set<FiledType>
*/
public static <T, FiledType> Set<FiledType> getSet(List<T> list, String fieldName) {
if (CollectionUtils.isEmpty(list)) {
return null;
}
Set<FiledType> set = new HashSet<FiledType>();
for (T t : list) {
FiledType fieldValue;
try {
fieldValue = MyCollectionUtil.getProperty(t, fieldName);
set.add(fieldValue);
} catch (Exception e) {
}
}
return set;
}
/**
* 从list提取字段根据function业务逻辑,生成一个Set集合,并返回
*
* @param list
* @param function
* @return Set<E>
*/
public static <T, E> Set<E> getSet(List<T> list, Function<T, E> function) {
if (CollectionUtils.isEmpty(list)) {
return null;
}
Set<E> set = new HashSet<E>();
for (T t : list) {
try {
set.add(function.apply(t));
} catch (Exception e) {
}
}
return set;
}
}