ProjectUtils.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package cn.com.ty.lift.common.utils;
  2. import cn.com.ty.lift.common.model.StatisticsKVModel;
  3. import cn.hutool.core.util.StrUtil;
  4. import java.io.*;
  5. import java.lang.reflect.Method;
  6. import java.util.*;
  7. /**
  8. * @author huangyuan
  9. * @date 2019-12-02
  10. * @description 项目工具类
  11. */
  12. public class ProjectUtils {
  13. /**
  14. * @param
  15. * @return
  16. * @description 通过id字符串,获取long型id列表
  17. * @date 2019/11/27 10:03 AM
  18. */
  19. public static List<Long> getLongIdFromIds(String ids) {
  20. List<Long> longIdList = new ArrayList<>();
  21. if (StrUtil.isNotBlank(ids)) {
  22. String[] idArray = ids.split(",");
  23. for (String id : idArray) {
  24. longIdList.add(Long.parseLong(id));
  25. }
  26. }
  27. return longIdList;
  28. }
  29. /**
  30. * @param ids id连接的字符串
  31. * @return long型id数组
  32. * @description 从id连接数组中获取id数组
  33. * @date 2020/4/26 1:10 下午
  34. */
  35. public static long[] getLongIdArrayFromIds(String ids) {
  36. long[] idArray = new long[0];
  37. if (StrUtil.isNotBlank(ids)) {
  38. String[] idStrArray = ids.split(",");
  39. idArray = new long[idStrArray.length];
  40. int i = 0;
  41. for (String id : idStrArray) {
  42. idArray[i++] = Long.parseLong(id);
  43. }
  44. }
  45. return idArray;
  46. }
  47. /**
  48. * @param array 数组
  49. * @return 通过","连接的字符串
  50. * @description 将数组值通过","连接
  51. * @date 2020/4/26 11:39 上午
  52. */
  53. public static String arrayToStrByComma(long[] array) {
  54. if (array != null && array.length > 0) {
  55. StringBuilder sb = new StringBuilder();
  56. for (Object o : array) {
  57. sb.append(o.toString()).append(",");
  58. }
  59. return sb.toString();
  60. }
  61. return null;
  62. }
  63. /**
  64. * 将对象集合转化为attr->List<Obj>的map形式,通过特定属性给对象归类
  65. *
  66. * @param objList
  67. * @param attrName:属性名
  68. * @param parameterTypes
  69. * @return Map
  70. * @throws
  71. * @author huangy
  72. * @date 2018年12月19日
  73. */
  74. public static Map attrToListMap(List objList, String attrName, Class<?>... parameterTypes) {
  75. Map map = new HashMap<>();
  76. try {
  77. if (objList != null && objList.size() > 0) {
  78. for (Object o : objList) {
  79. Class<?> classObj = o.getClass();
  80. //获取属性get方法
  81. String methodName = PojoUtils.attrGetMethodName(attrName);
  82. Method method = classObj.getMethod(methodName, parameterTypes);
  83. Object attr = method.invoke(o, parameterTypes);
  84. //在map中获取对象集合
  85. List objInfoList = (List) map.get(attr);
  86. if (objInfoList == null) {
  87. objInfoList = new ArrayList();
  88. }
  89. objInfoList.add(o);
  90. map.put(attr, objInfoList);
  91. }
  92. }
  93. } catch (Exception e) {
  94. e.printStackTrace();
  95. }
  96. return map;
  97. }
  98. /**
  99. * 将对象集合转化为attribute->Object的map
  100. *
  101. * @param objList
  102. * @param attrName:属性名称
  103. * @param parameterTypes
  104. * @return Map
  105. * @throws
  106. * @author huangy
  107. * @date 2018年12月19日
  108. */
  109. public static Map attrToObjMap(List objList, String attrName, Class<?>... parameterTypes) {
  110. Map<Object, Object> map = new HashMap<>();
  111. try {
  112. if (objList != null && objList.size() > 0) {
  113. for (Object o : objList) {
  114. Class<?> classObj = o.getClass();
  115. Method method = classObj.getMethod(PojoUtils.attrGetMethodName(attrName), parameterTypes);
  116. Object attr = method.invoke(o, parameterTypes);
  117. map.put(attr, o);
  118. }
  119. }
  120. } catch (Exception e) {
  121. e.printStackTrace();
  122. }
  123. return map;
  124. }
  125. /**
  126. * 通过集合对象集合获取对象id集合
  127. *
  128. * @param objList
  129. * @param attrName
  130. * @param parameterTypes
  131. * @return List<? extends Object>
  132. * @throws
  133. * @author huangy
  134. */
  135. public static List getAttrList(List objList, String attrName, Class<?>... parameterTypes) {
  136. List idList = new ArrayList();
  137. if (objList != null && objList.size() > 0) {
  138. for (Object o : objList) {
  139. try {
  140. Class<?> classObj = o.getClass();
  141. Method method = classObj.getMethod(PojoUtils.attrGetMethodName(attrName), parameterTypes);
  142. Object id = method.invoke(o, parameterTypes);
  143. idList.add(id);
  144. } catch (Exception e) {
  145. e.printStackTrace();
  146. }
  147. }
  148. }
  149. return idList;
  150. }
  151. /**
  152. * 列表去重
  153. *
  154. * @param list void
  155. * @throws
  156. */
  157. public static void removeDuplicateWithOrder(List list) {
  158. Set set = new HashSet<>();
  159. List newList = new ArrayList<>();
  160. if (list != null && list.size() > 0) {
  161. for (Iterator<Object> iter = list.iterator(); iter.hasNext(); ) {
  162. Object element = iter.next();
  163. if (set.add(element)) {
  164. newList.add(element);
  165. }
  166. }
  167. list.clear();
  168. list.addAll(newList);
  169. }
  170. }
  171. /**
  172. * @param
  173. * @return
  174. * @description 将map值转化为 key -> key值 value -> value值
  175. * @date 2020/2/24 7:37 下午
  176. */
  177. public static List<StatisticsKVModel> transReturnMapToStatisticsKVModel(Map dataMap) {
  178. List<StatisticsKVModel> statisticsKVModelList = new ArrayList<>();
  179. if (dataMap != null && dataMap.size() > 0) {
  180. dataMap.forEach((key, value) -> {
  181. StatisticsKVModel outStatisticsKVModel = new StatisticsKVModel();
  182. //设置键
  183. outStatisticsKVModel.setKey(key);
  184. //判断值是否为也为map如果是就行封装 目前只涉及一层封装
  185. if (value != null && value instanceof Map) {
  186. outStatisticsKVModel.setValue(transReturnMapToStatisticsKVModel((Map) value));
  187. } else {
  188. outStatisticsKVModel.setValue(value);
  189. }
  190. statisticsKVModelList.add(outStatisticsKVModel);
  191. });
  192. }
  193. return statisticsKVModelList;
  194. }
  195. /**
  196. * @param list 要拷贝的list
  197. * @return 返回拷贝后的对象
  198. * @description 对集合进行拷贝
  199. * @date 2020/4/16 5:05 下午
  200. */
  201. @SuppressWarnings("unchecked")
  202. public static <T> List<T> deepCopy(List<T> list) {
  203. List<T> dest = new ArrayList<>();
  204. try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream()) {
  205. ObjectOutputStream out = new ObjectOutputStream(byteOut);
  206. out.writeObject(list);
  207. ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
  208. ObjectInputStream in = new ObjectInputStream(byteIn);
  209. dest = (List<T>) in.readObject();
  210. } catch (IOException | ClassNotFoundException e) {
  211. e.printStackTrace();
  212. }
  213. return dest;
  214. }
  215. }