激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java轉樹形結構工具類詳解

java轉樹形結構工具類詳解

2020-08-26 10:01方闕 Java教程

這篇文章主要為大家詳細介紹了java轉樹形結構工具類,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java轉樹形結構工具類的具體代碼,供大家參考,具體內容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.ToString;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
 
import java.lang.reflect.Field;
import java.util.*;
 
/**
 * @author : liyk
 * @version 1.0
 * @date : 2020/6/9
 */
public class TreeUtil {
 
 
  /**
   * 將 List 轉為樹形結構
   *
   * @param origList     : 要轉換的 List
   * @param idFieldName    : id字段名
   * @param parentIdFieldName : parentId 字段名
   * @param childrenFieldName : children 字段名
   * @param <T>        : 擁有父子結構的 Entity
   * @return : 樹形結果
   * @throws Exception .
   */
  public static <T> List<T> convert(List<T> origList, String idFieldName,
                   String parentIdFieldName, String childrenFieldName) throws Exception {
    // 用于保存當前 id 索引的實體類
    Map<String, T> idMaps = new HashMap<>();
    // 暫存區(qū), 用于保存沒有找到父 id 的控件
    List<T> tempList = new ArrayList<>();
    List<T> result = new ArrayList<>();
    for (T entity : origList) {
      // 獲取 id, parentId, children
      String id = Objects.toString(getFieldValue(entity, idFieldName), "");
      String parentId = Objects.toString(getFieldValue(entity, parentIdFieldName), "");
      if (StringUtils.isEmpty(id)) {
        throw new Exception("存在id為空的資料");
      }
      idMaps.put(id, entity);
      if (StringUtils.isEmpty(parentId)) {
        // 如果父 id 為空, 則實體類為第一層
        result.add(entity);
      } else {
        // 根據父 id 獲取實體類
        T parentEntity = idMaps.get(parentId);
        if (parentEntity == null) {
          // 沒找到先放入暫存區(qū)
          tempList.add(entity);
        } else {
          // 父組件判斷是否存在 children, 不存在新增, 存在則直接假如
          setChildrenValue(childrenFieldName, entity, parentEntity);
        }
      }
    }
    // 處理暫存區(qū), 暫存區(qū)的一定不為根節(jié)點, 所以它只要父節(jié)點存在, 那么此輪查詢一定能找到父節(jié)點(上一輪已經將全部節(jié)點放入 idMaps)
    for (T entity : tempList) {
      // 獲取 parentId
      String parentId = Objects.toString(getFieldValue(entity, parentIdFieldName), "");
      // 根據父id獲取實體類
      T parentEntity = idMaps.get(parentId);
      if (parentEntity == null) {
        throw new Exception("存在孤立的子節(jié)點");
      } else {
        // 父組件判斷是否存在children, 不存在新增, 存在則直接假如
        setChildrenValue(childrenFieldName, entity, parentEntity);
      }
    }
    return result;
  }
 
  private static <T> void setChildrenValue(String childrenFieldName, T entity, T parentEntity) throws Exception {
    Object children = getFieldValue(parentEntity, childrenFieldName);
    List<T> childrenList;
    if (children == null) {
      childrenList = new ArrayList<>();
      childrenList.add(entity);
      setFieldValue(parentEntity, childrenFieldName, childrenList);
    } else {
      List<T> childrenReal = (List<T>) children;
      childrenReal.add(entity);
    }
  }
 
  private static <T> Object getFieldValue(T entity, String fieldName) throws Exception {
    Field field = ReflectionUtils.findField(entity.getClass(), fieldName);
    if (field == null) {
      throw new Exception(String.format("字段名稱[%s]不存在", fieldName));
    }
    boolean accessible = field.isAccessible();
    field.setAccessible(true);
    Object result = ReflectionUtils.getField(field, entity);
    field.setAccessible(accessible);
    return result;
  }
 
  private static <T> void setFieldValue(T entity, String fieldName, Object value) throws Exception {
    Field field = ReflectionUtils.findField(entity.getClass(), fieldName);
    if (field == null) {
      throw new Exception(String.format("字段名稱[%s]不存在", fieldName));
    }
    boolean accessible = field.isAccessible();
    field.setAccessible(true);
    ReflectionUtils.setField(field, entity, value);
    field.setAccessible(accessible);
  }
 
  public static void main(String[] args) throws Exception {
    List<Demo> list = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
      Demo demo = new Demo(i, "一級節(jié)點" + i);
      list.add(demo);
    }
    for (int i = 5; i < 15; i++) {
      Demo demo = new Demo(i, i % 5, "二級節(jié)點" + i);
      list.add(demo);
    }
    for (int i = 15; i < 100; i++) {
      Demo demo = new Demo(i, i % 10 + 5, "三級節(jié)點" + i);
      list.add(demo);
    }
    Demo demo = new Demo(100, 102, "非法節(jié)點");
    list.add(demo);
    List<Demo> convert = TreeUtil.convert(list, "id", "pid", "children");
    String s = JSON.toJSONString(convert);
    System.out.println(s);
  }
 
}
 
@Data
@ToString
class Demo {
  private Integer id;
  private Integer pid;
  private String name;
  private List<Demo> children;
 
  public Demo(Integer id, Integer pid, String name) {
    this.id = id;
    this.pid = pid;
    this.name = name;
  }
 
  public Demo(Integer id, String name) {
    this.id = id;
    this.name = name;
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/l707268743/article/details/106642018

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产va在线观看免费 | 亚洲成人入口 | 成人小视频免费在线观看 | h视频免费观看 | 美女久久久久 | 中国性xxx | 久久人人爽爽爽人久久久 | 国产色视频免费 | 一区二区三区四区在线 | 久久精品视频在线免费观看 | 免费a视频| 欧美成人免费一区二区三区 | 一级电影在线观看 | 毛片在线免费视频 | 狠狠操夜夜爱 | 91网址在线播放 | 在线看免费观看av | 久久精热 | 亚洲成人欧美 | 免看一级片 | 黄色影院网站 | 欧美特级一级毛片 | 国产成人综合在线视频 | 久久欧美亚洲另类专区91大神 | 久久人人爽人人爽人人片av高请 | 91久久久久久久久久久久久久 | 午夜精品视频在线 | 久久久无码精品亚洲日韩按摩 | av成人免费看 | 免费观看黄色一级视频 | 国产成人精品网站 | 国产91丝袜在线熟 | 草人人| 欧美性生活久久久 | 亚洲成人免费网站 | 日韩毛片网站 | 调教小男生抽打尿孔嗯啊视频 | 国产精品夜色视频一级区 | 91羞羞 | 成人午夜高清 | 97人操|