ProjectUtils.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.lang.reflect.Method;
  5. import java.util.*;
  6. /**
  7. * @author huangyuan
  8. * @date 2019-12-02
  9. * @description 项目工具类
  10. */
  11. public class ProjectUtils {
  12. /**
  13. * @param
  14. * @return
  15. * @description 通过id字符串,获取long型id列表
  16. * @date 2019/11/27 10:03 AM
  17. */
  18. public static List<Long> getLongIdFromIds(String ids) {
  19. List<Long> longIdList = new ArrayList<>();
  20. if (StrUtil.isNotBlank(ids)) {
  21. String[] idArray = ids.split(",");
  22. for (String id : idArray) {
  23. longIdList.add(Long.parseLong(id));
  24. }
  25. }
  26. return longIdList;
  27. }
  28. /**
  29. * 将对象集合转化为attr->List<Obj>的map形式,通过特定属性给对象归类
  30. *
  31. * @param objList
  32. * @param attrName:属性名
  33. * @param parameterTypes
  34. * @return Map
  35. * @throws
  36. * @author huangy
  37. * @date 2018年12月19日
  38. */
  39. public static Map attrToListMap(List objList, String attrName, Class<?>... parameterTypes) {
  40. Map map = new HashMap<>();
  41. try {
  42. if (objList != null && objList.size() > 0) {
  43. for (Object o : objList) {
  44. Class<?> classObj = o.getClass();
  45. //获取属性get方法
  46. String methodName = PojoUtils.attrGetMethodName(attrName);
  47. Method method = classObj.getMethod(methodName, parameterTypes);
  48. Object attr = method.invoke(o, parameterTypes);
  49. //在map中获取对象集合
  50. List objInfoList = (List) map.get(attr);
  51. if (objInfoList == null) {
  52. objInfoList = new ArrayList();
  53. }
  54. objInfoList.add(o);
  55. map.put(attr, objInfoList);
  56. }
  57. }
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. }
  61. return map;
  62. }
  63. /**
  64. * 将对象集合转化为attribute->Object的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 attrToObjMap(List objList, String attrName, Class<?>... parameterTypes) {
  75. Map<Object, Object> map = new HashMap<>();
  76. try {
  77. if (objList != null && objList.size() > 0) {
  78. for (Object o : objList) {
  79. Class<?> classObj = o.getClass();
  80. Method method = classObj.getMethod(PojoUtils.attrGetMethodName(attrName), parameterTypes);
  81. Object attr = method.invoke(o, parameterTypes);
  82. map.put(attr, o);
  83. }
  84. }
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. }
  88. return map;
  89. }
  90. /**
  91. * 通过集合对象集合获取对象id集合
  92. *
  93. * @param objList
  94. * @param attrName
  95. * @param parameterTypes
  96. * @return List<? extends Object>
  97. * @throws
  98. * @author huangy
  99. */
  100. public static List getAttrList(List objList, String attrName, Class<?>... parameterTypes) {
  101. List idList = new ArrayList();
  102. if (objList != null && objList.size() > 0) {
  103. for (Object o : objList) {
  104. try {
  105. Class<?> classObj = o.getClass();
  106. Method method = classObj.getMethod(PojoUtils.attrGetMethodName(attrName), parameterTypes);
  107. Object id = method.invoke(o, parameterTypes);
  108. idList.add(id);
  109. } catch (Exception e) {
  110. e.printStackTrace();
  111. }
  112. }
  113. }
  114. return idList;
  115. }
  116. /**
  117. * 列表去重
  118. *
  119. * @param list void
  120. * @throws
  121. */
  122. public static void removeDuplicateWithOrder(List list) {
  123. Set set = new HashSet<>();
  124. List newList = new ArrayList<>();
  125. if (list != null && list.size() > 0) {
  126. for (Iterator<Object> iter = list.iterator(); iter.hasNext(); ) {
  127. Object element = iter.next();
  128. if (set.add(element)) {
  129. newList.add(element);
  130. }
  131. }
  132. list.clear();
  133. list.addAll(newList);
  134. }
  135. }
  136. /**
  137. * @param
  138. * @return
  139. * @description 将map值转化为 key -> key值 value -> value值
  140. * @date 2020/2/24 7:37 下午
  141. */
  142. public static List<StatisticsKVModel> transReturnMapToStatisticsKVModel(Map dataMap) {
  143. List<StatisticsKVModel> statisticsKVModelList = new ArrayList<>();
  144. if (dataMap != null && dataMap.size() > 0) {
  145. dataMap.forEach((key, value) -> {
  146. StatisticsKVModel outStatisticsKVModel = new StatisticsKVModel();
  147. //设置键
  148. outStatisticsKVModel.setKey(key);
  149. //判断值是否为也为map如果是就行封装 目前只涉及一层封装
  150. if (value != null && value instanceof Map) {
  151. outStatisticsKVModel.setValue(transReturnMapToStatisticsKVModel((Map)value));
  152. } else {
  153. outStatisticsKVModel.setValue(value);
  154. }
  155. statisticsKVModelList.add(outStatisticsKVModel);
  156. });
  157. }
  158. return statisticsKVModelList;
  159. }
  160. }