123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- package cn.com.ty.lift.common.utils;
- import cn.com.ty.lift.common.model.StatisticsKVModel;
- import cn.hutool.core.util.StrUtil;
- import java.io.*;
- 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;
- }
- /**
- * @param ids id连接的字符串
- * @return long型id数组
- * @description 从id连接数组中获取id数组
- * @date 2020/4/26 1:10 下午
- */
- public static long[] getLongIdArrayFromIds(String ids) {
- long[] idArray = new long[0];
- if (StrUtil.isNotBlank(ids)) {
- String[] idStrArray = ids.split(",");
- idArray = new long[idStrArray.length];
- int i = 0;
- for (String id : idStrArray) {
- idArray[i++] = Long.parseLong(id);
- }
- }
- return idArray;
- }
- /**
- * @param array 数组
- * @return 通过","连接的字符串
- * @description 将数组值通过","连接
- * @date 2020/4/26 11:39 上午
- */
- public static String arrayToStrByComma(long[] array) {
- if (array != null && array.length > 0) {
- StringBuilder sb = new StringBuilder();
- for (Object o : array) {
- sb.append(o.toString()).append(",");
- }
- return sb.toString();
- }
- return null;
- }
- /**
- * 将对象集合转化为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;
- }
- /**
- * @param list 要拷贝的list
- * @return 返回拷贝后的对象
- * @description 对集合进行拷贝
- * @date 2020/4/16 5:05 下午
- */
- @SuppressWarnings("unchecked")
- public static <T> List<T> deepCopy(List<T> list) {
- List<T> dest = new ArrayList<>();
- try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream()) {
- ObjectOutputStream out = new ObjectOutputStream(byteOut);
- out.writeObject(list);
- ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
- ObjectInputStream in = new ObjectInputStream(byteIn);
- dest = (List<T>) in.readObject();
- } catch (IOException | ClassNotFoundException e) {
- e.printStackTrace();
- }
- return dest;
- }
- }
|