|
@@ -1,27 +1,30 @@
|
|
|
package com.fjhx.utils;
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
-import cn.hutool.core.util.ReflectUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
|
-import java.lang.reflect.Field;
|
|
|
-import java.util.*;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
public class TreeUtil {
|
|
|
|
|
|
|
|
|
* 节点ID名称
|
|
|
*/
|
|
|
- private static final String ID_NAME = "id";
|
|
|
+ public static final String ID_NAME = "id";
|
|
|
|
|
|
|
|
|
* 节点名称
|
|
|
*/
|
|
|
- private static final String LABEL_NAME = "label";
|
|
|
+ public static final String LABEL_NAME = "label";
|
|
|
|
|
|
|
|
|
* 子节点名称
|
|
|
*/
|
|
|
- private static final String CHILDREN_NAME = "children";
|
|
|
+ public static final String CHILDREN_NAME = "children";
|
|
|
|
|
|
|
|
|
* 父id名称
|
|
@@ -29,27 +32,14 @@ public class TreeUtil {
|
|
|
private static final String PARENT_ID_NAME = "parentId";
|
|
|
|
|
|
|
|
|
- * 构建树形列表(父节点属性名称默认:parentId)
|
|
|
- *
|
|
|
- * @param labelName 需要构建的节点名称
|
|
|
- * @param objList 需要构建的数组
|
|
|
- * @return 树形map
|
|
|
- */
|
|
|
- public static <T> List<Map<String, Object>> buildTree(String labelName, List<T> objList) {
|
|
|
- return buildTree(ID_NAME, labelName, PARENT_ID_NAME, objList);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- * 构建树形列表(父节点属性名称默认:parentId)
|
|
|
+ * 构建树形列表
|
|
|
*
|
|
|
- * @param idName 需要构建的节点ID名称
|
|
|
* @param labelName 需要构建的节点名称
|
|
|
- * @param objList 需要构建的数组
|
|
|
+ * @param list 需要构建的数组
|
|
|
* @return 树形map
|
|
|
*/
|
|
|
- public static <T> List<Map<String, Object>> buildTree(String idName, String labelName, List<T> objList) {
|
|
|
- return buildTree(idName, labelName, PARENT_ID_NAME, objList);
|
|
|
+ public static List<JSONObject> buildTree(String labelName, List<?> list) {
|
|
|
+ return buildTree(ID_NAME, labelName, PARENT_ID_NAME, list);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -58,141 +48,62 @@ public class TreeUtil {
|
|
|
* @param idName 需要构建的节点ID名称
|
|
|
* @param labelName 需要构建的节点名称
|
|
|
* @param parentIdName 需要构建的节点父节点名称
|
|
|
- * @param objList 需要构建的数组
|
|
|
+ * @param list 需要构建的数组
|
|
|
* @return 树形map
|
|
|
*/
|
|
|
- public static <T> List<Map<String, Object>> buildTree(
|
|
|
- String idName, String labelName, String parentIdName, List<T> objList) {
|
|
|
-
|
|
|
- List<Map<String, Object>> tempMapList = new ArrayList<>();
|
|
|
-
|
|
|
-
|
|
|
- List<Object> tempIdValue = new ArrayList<>();
|
|
|
-
|
|
|
- for (T t : objList) {
|
|
|
- Object idValue = HxUtil.reflectGet(t, idName);
|
|
|
- tempIdValue.add(idValue);
|
|
|
-
|
|
|
- Map<String, Object> tempMap = new HashMap<>();
|
|
|
- tempMap.put(ID_NAME, idValue);
|
|
|
- tempMap.put(LABEL_NAME, HxUtil.reflectGet(t, labelName));
|
|
|
- tempMap.put(PARENT_ID_NAME, HxUtil.reflectGet(t, parentIdName));
|
|
|
- tempMapList.add(tempMap);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- List<Map<String, Object>> mapList = new ArrayList<>();
|
|
|
-
|
|
|
- for (Map<String, Object> item : tempMapList) {
|
|
|
- if (!tempIdValue.contains(item.get(PARENT_ID_NAME))) {
|
|
|
- Map<String, Object> map = createNode(item, tempMapList);
|
|
|
- mapList.add(map);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return mapList;
|
|
|
+ public static List<JSONObject> buildTree(String idName, String labelName, String parentIdName, List<?> list) {
|
|
|
+ List<JSONObject> collect = list.stream().map(item -> {
|
|
|
+ JSONObject data = JSON.parseObject(JSONObject.toJSONString(item));
|
|
|
+
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put(ID_NAME, data.get(idName));
|
|
|
+ jsonObject.put(PARENT_ID_NAME, data.get(parentIdName));
|
|
|
+ jsonObject.put(LABEL_NAME, data.get(labelName));
|
|
|
+
|
|
|
+ return jsonObject;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ return buildTreeObj(collect);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- * 递归找出子节点
|
|
|
- */
|
|
|
- private static List<Map<String, Object>> recursionGetChild(List<Map<String, Object>> tempMapList, Object parentId) {
|
|
|
- List<Map<String, Object>> mapList = new ArrayList<>();
|
|
|
-
|
|
|
- for (Map<String, Object> tempMap : tempMapList) {
|
|
|
- if (tempMap.get(PARENT_ID_NAME).equals(parentId)) {
|
|
|
- Map<String, Object> map = createNode(tempMap, tempMapList);
|
|
|
- mapList.add(map);
|
|
|
- }
|
|
|
- }
|
|
|
- return mapList;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * 创建一个节点
|
|
|
- */
|
|
|
- private static Map<String, Object> createNode(Map<String, Object> tempMap, List<Map<String, Object>> tempMapList) {
|
|
|
- Map<String, Object> map = new LinkedHashMap<>();
|
|
|
- map.put(ID_NAME, tempMap.get(ID_NAME));
|
|
|
- map.put(LABEL_NAME, tempMap.get(LABEL_NAME));
|
|
|
- List<Map<String, Object>> child = recursionGetChild(tempMapList, tempMap.get(ID_NAME));
|
|
|
- if (child.size() > 0) {
|
|
|
- map.put(CHILDREN_NAME, child);
|
|
|
- }
|
|
|
- return map;
|
|
|
+ public static List<JSONObject> buildTreeObj(List<?> list) {
|
|
|
+ return buildTreeObj(ID_NAME, PARENT_ID_NAME, CHILDREN_NAME, list);
|
|
|
}
|
|
|
|
|
|
- public static List<Map<String, Object>> buildTreeObj(List<?> list) {
|
|
|
- return buildTreeObj(ID_NAME, PARENT_ID_NAME, list);
|
|
|
- }
|
|
|
-
|
|
|
- public static List<Map<String, Object>> buildTreeObj(String idName, String parentName, List<?> list) {
|
|
|
- List<Map<String, Object>> result = new ArrayList<>();
|
|
|
-
|
|
|
-
|
|
|
- List<Map<String, Object>> tempMapList = new ArrayList<>();
|
|
|
-
|
|
|
-
|
|
|
- Set<Object> idSet = new HashSet<>();
|
|
|
-
|
|
|
-
|
|
|
- for (Object object : list) {
|
|
|
- Map<String, Object> tempMap = new LinkedHashMap<>();
|
|
|
-
|
|
|
- Field[] fields = ReflectUtil.getFields(object.getClass());
|
|
|
-
|
|
|
- for (Field field : fields) {
|
|
|
- if (field.getName().equals("serialVersionUID")) {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- field.setAccessible(true);
|
|
|
- String key = field.getName();
|
|
|
- Object value = HxUtil.reflectGet(object, key);
|
|
|
-
|
|
|
- if (key.equals(idName) && ObjectUtil.isNotEmpty(value)) {
|
|
|
- idSet.add(value);
|
|
|
- }
|
|
|
-
|
|
|
- tempMap.put(key, value);
|
|
|
- }
|
|
|
- tempMapList.add(tempMap);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- for (Map<String, Object> map : tempMapList) {
|
|
|
-
|
|
|
- Object parentId = map.get(parentName);
|
|
|
- if (parentId != null && !idSet.contains(parentId)) {
|
|
|
-
|
|
|
- LookingForChildren(tempMapList, map, idName, parentName);
|
|
|
- result.add(map);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return result;
|
|
|
+ public static List<JSONObject> buildTreeObj(String idName, String parentName, String childrenName, List<?> list) {
|
|
|
+
|
|
|
+
|
|
|
+ List<JSONObject> dataList = JSONArray.parseArray(JSONObject.toJSONString(list), JSONObject.class);
|
|
|
+
|
|
|
+
|
|
|
+ Map<String, JSONObject> idDataMap = dataList.stream().collect(Collectors.toMap(
|
|
|
+ item -> item.getString(idName),
|
|
|
+ item -> item,
|
|
|
+ (v1, v2) -> v1
|
|
|
+ ));
|
|
|
+
|
|
|
+
|
|
|
+ return dataList.stream()
|
|
|
+ .filter(data -> null == idDataMap.get(data.getString(parentName)))
|
|
|
+ .peek(data -> {
|
|
|
+
|
|
|
+ List<JSONObject> childNodes = findChildNodes(dataList, idName, parentName, childrenName, data.getString(idName));
|
|
|
+ if (childNodes.size() > 0) {
|
|
|
+ data.put(childrenName, childNodes);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- private static void LookingForChildren(List<Map<String, Object>> tempMapList, Map<String, Object> map, String idName, String parentName) {
|
|
|
-
|
|
|
- ArrayList<Map<String, Object>> childrenList = new ArrayList<>();
|
|
|
- for (Map<String, Object> tempMap : tempMapList) {
|
|
|
-
|
|
|
- Object id = map.get(idName);
|
|
|
- Object parentId = tempMap.get(parentName);
|
|
|
-
|
|
|
- if (ObjectUtil.isAllNotEmpty(id, parentId) && id.equals(parentId)) {
|
|
|
-
|
|
|
- LookingForChildren(tempMapList, tempMap, idName, parentName);
|
|
|
- childrenList.add(tempMap);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (childrenList.size() > 0) {
|
|
|
- map.put(CHILDREN_NAME, childrenList);
|
|
|
- }
|
|
|
+ private static List<JSONObject> findChildNodes(List<JSONObject> dataList, String idName, String parentName, String childrenName, String id) {
|
|
|
+ return dataList.stream()
|
|
|
+ .filter(data -> ObjectUtil.equals(data.getString(parentName), id))
|
|
|
+ .peek(data -> {
|
|
|
+ List<JSONObject> childNodes = findChildNodes(dataList, idName, parentName, childrenName, data.getString(idName));
|
|
|
+ if (childNodes.size() > 0) {
|
|
|
+ data.put(childrenName, childNodes);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
}
|