123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- package cn.com.ty.lift.common.utils;
- import cn.com.ty.lift.common.model.StatisticsKVModel;
- import cn.hutool.core.util.StrUtil;
- import java.lang.reflect.Method;
- import java.util.*;
- /**
- * @author huangyuan
- * @date 2019-12-02
- * @description 项目工具类
- */
- public class ProjectUtils {
- /**
- * @param
- * @return
- * @description 通过id字符串,获取long型id列表
- * @date 2019/11/27 10:03 AM
- */
- public static List<Long> getLongIdFromIds(String ids) {
- List<Long> longIdList = new ArrayList<>();
- if (StrUtil.isNotBlank(ids)) {
- String[] idArray = ids.split(",");
- for (String id : idArray) {
- longIdList.add(Long.parseLong(id));
- }
- }
- return longIdList;
- }
- /**
- * 将对象集合转化为attr->List<Obj>的map形式,通过特定属性给对象归类
- *
- * @param objList
- * @param attrName:属性名
- * @param parameterTypes
- * @return Map
- * @throws
- * @author huangy
- * @date 2018年12月19日
- */
- public static Map attrToListMap(List objList, String attrName, Class<?>... parameterTypes) {
- Map map = new HashMap<>();
- try {
- if (objList != null && objList.size() > 0) {
- for (Object o : objList) {
- Class<?> classObj = o.getClass();
- //获取属性get方法
- String methodName = PojoUtils.attrGetMethodName(attrName);
- Method method = classObj.getMethod(methodName, parameterTypes);
- Object attr = method.invoke(o, parameterTypes);
- //在map中获取对象集合
- List objInfoList = (List) map.get(attr);
- if (objInfoList == null) {
- objInfoList = new ArrayList();
- }
- objInfoList.add(o);
- map.put(attr, objInfoList);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return map;
- }
- /**
- * 将对象集合转化为attribute->Object的map
- *
- * @param objList
- * @param attrName:属性名称
- * @param parameterTypes
- * @return Map
- * @throws
- * @author huangy
- * @date 2018年12月19日
- */
- public static Map attrToObjMap(List objList, String attrName, Class<?>... parameterTypes) {
- Map<Object, Object> map = new HashMap<>();
- try {
- if (objList != null && objList.size() > 0) {
- for (Object o : objList) {
- Class<?> classObj = o.getClass();
- Method method = classObj.getMethod(PojoUtils.attrGetMethodName(attrName), parameterTypes);
- Object attr = method.invoke(o, parameterTypes);
- map.put(attr, o);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return map;
- }
- /**
- * 通过集合对象集合获取对象id集合
- *
- * @param objList
- * @param attrName
- * @param parameterTypes
- * @return List<? extends Object>
- * @throws
- * @author huangy
- */
- public static List getAttrList(List objList, String attrName, Class<?>... parameterTypes) {
- List idList = new ArrayList();
- if (objList != null && objList.size() > 0) {
- for (Object o : objList) {
- try {
- Class<?> classObj = o.getClass();
- Method method = classObj.getMethod(PojoUtils.attrGetMethodName(attrName), parameterTypes);
- Object id = method.invoke(o, parameterTypes);
- idList.add(id);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- return idList;
- }
- /**
- * 列表去重
- *
- * @param list void
- * @throws
- */
- public static void removeDuplicateWithOrder(List list) {
- Set set = new HashSet<>();
- List newList = new ArrayList<>();
- if (list != null && list.size() > 0) {
- for (Iterator<Object> iter = list.iterator(); iter.hasNext(); ) {
- Object element = iter.next();
- if (set.add(element)) {
- newList.add(element);
- }
- }
- list.clear();
- list.addAll(newList);
- }
- }
- /**
- * @param
- * @return
- * @description 将map值转化为 key -> key值 value -> value值
- * @date 2020/2/24 7:37 下午
- */
- public static List<StatisticsKVModel> transReturnMapToStatisticsKVModel(Map dataMap) {
- List<StatisticsKVModel> statisticsKVModelList = new ArrayList<>();
- if (dataMap != null && dataMap.size() > 0) {
- dataMap.forEach((key, value) -> {
- StatisticsKVModel outStatisticsKVModel = new StatisticsKVModel();
- //设置键
- outStatisticsKVModel.setKey(key);
- //判断值是否为也为map如果是就行封装 目前只涉及一层封装
- if (value != null && value instanceof Map) {
- outStatisticsKVModel.setValue(transReturnMapToStatisticsKVModel((Map)value));
- } else {
- outStatisticsKVModel.setValue(value);
- }
- statisticsKVModelList.add(outStatisticsKVModel);
- });
- }
- return statisticsKVModelList;
- }
- }
|