java 遞迴轉非遞迴(樹形轉非樹形)
阿新 • • 發佈:2022-05-25
1.情景展示
接著上一篇講,在上一篇我們已經將資料通過遞迴轉成了樹形結構。
如何將樹形結構轉非樹形結構?(仍然按照樹形層級關係進行平鋪顯示)
檢視程式碼
[{ name = 根節點, childList = [{ name = 一級節點1, childList = [{ name = 二級節點1.1, childList = [], id = 3, parentId = 2 }, { name = 二級節點1.2, childList = [], id = 4, parentId = 2 }, { name = 二級節點1.3, childList = [], id = 5, parentId = 2 } ], id = 2, parentId = 1 }, { name = 一級節點2, childList = [{ name = 二級節點2.1, childList = [{ name = 三級節點2.1.1, childList = [], id = 9, parentId = 7 }, { name = 三節點2.1.2, childList = [], id = 10, parentId = 7 } ], id = 7, parentId = 6 }, { name = 二級節點2.2, childList = [], id = 8, parentId = 6 } ], id = 6, parentId = 1 }, { name = 一級節點3, childList = [{ name = 二級節點3.1, childList = [{ name = 三級節點3.1.1, childList = [{ name = 四級節點3.1.1.1, childList = [{ name = 五級節點3.1.1.1.1, childList = [], id = 15, parentId = 14 } ], id = 14, parentId = 13 } ], id = 13, parentId = 12 } ], id = 12, parentId = 11 } ], id = 11, parentId = 1 } ], id = 1, parentId = 0 } ]
2.具體分析
可以通過棧Stack來完成。
3.解決方案
程式碼實現
/* * 將父節點下的子節點進行由巢狀改成平鋪 * @description: 按照層級關係,將巢狀改成平鋪 * @param: root 巢狀資料 * @return: java.util.ArrayList<java.util.Map<java.lang.String,java.lang.Object>> * 平鋪資料 */ public static ArrayList<Map<String, Object>> tileTraversal(Map<String, Object> root) { ArrayList<Map<String, Object>> res = new ArrayList<>(); if (root == null) { return res; } Stack<Map<String, Object>> stack = new Stack<>(); stack.push(root); while (!stack.empty()) { Map<String, Object> temp = stack.pop(); Map<String, Object> copyMap = new HashMap<>(temp.size()); // 複製當前節點 copyMap.putAll(temp); // 移除map(該節點)當中的子節點 copyMap.remove("childList"); // 將此節點放到集合當中 res.add(copyMap); // 當前節點,存在子節點 if (temp.get("childList") != null && ((List<Map<String, Object>>) temp.get("childList")).size() > 0) { // ((List<Map<String, Object>>) temp.get("childList")).forEach(m -> stack.push(m)); List<Map<String, Object>> list = (List<Map<String, Object>>) temp.get("childList"); for (int i = list.size() - 1; i >= 0; i--) { stack.push(list.get(i)); } } } return res; }
呼叫
// mapList是樹形結構(已經存在的資料)
List<Map<String, Object>> tileList = new ArrayList<>();
mapList.forEach(m -> tileList.addAll(tileTraversal(m)));
System.out.println(tileList);
[{name=根節點, id=1, parentId=0}, {name=一級節點1, id=2, parentId=1}, {name=二級節點1.1, id=3, parentId=2}, {name=二級節點1.2, id=4, parentId=2}, {name=二級節點1.3, id=5, parentId=2}, {name=一級節點2, id=6, parentId=1}, {name=二級節點2.1, id=7, parentId=6}, {name=三級節點2.1.1, id=9, parentId=7}, {name=三節點2.1.2, id=10, parentId=7}, {name=二級節點2.2, id=8, parentId=6}, {name=一級節點3, id=11, parentId=1}, {name=二級節點3.1, id=12, parentId=11}, {name=三級節點3.1.1, id=13, parentId=12}, {name=四級節點3.1.1.1, id=14, parentId=13}, {name=五級節點3.1.1.1.1, id=15, parentId=14}]
4.效果展示
轉JSON
System.out.println(JSON.toJSON(tileList));
[{"name":"根節點","id":"1","parentId":"0"},{"name":"一級節點1","id":"2","parentId":"1"},{"name":"二級節點1.1","id":"3","parentId":"2"},{"name":"二級節點1.2","id":"4","parentId":"2"},{"name":"二級節點1.3","id":"5","parentId":"2"},{"name":"一級節點2","id":"6","parentId":"1"},{"name":"二級節點2.1","id":"7","parentId":"6"},{"name":"三級節點2.1.1","id":"9","parentId":"7"},{"name":"三節點2.1.2","id":"10","parentId":"7"},{"name":"二級節點2.2","id":"8","parentId":"6"},{"name":"一級節點3","id":"11","parentId":"1"},{"name":"二級節點3.1","id":"12","parentId":"11"},{"name":"三級節點3.1.1","id":"13","parentId":"12"},{"name":"四級節點3.1.1.1","id":"14","parentId":"13"},{"name":"五級節點3.1.1.1.1","id":"15","parentId":"14"}]
格式化
檢視程式碼
[{
"name": "根節點",
"id": "1",
"parentId": "0"
},
{
"name": "一級節點1",
"id": "2",
"parentId": "1"
},
{
"name": "二級節點1.1",
"id": "3",
"parentId": "2"
},
{
"name": "二級節點1.2",
"id": "4",
"parentId": "2"
},
{
"name": "二級節點1.3",
"id": "5",
"parentId": "2"
},
{
"name": "一級節點2",
"id": "6",
"parentId": "1"
},
{
"name": "二級節點2.1",
"id": "7",
"parentId": "6"
},
{
"name": "三級節點2.1.1",
"id": "9",
"parentId": "7"
},
{
"name": "三節點2.1.2",
"id": "10",
"parentId": "7"
},
{
"name": "二級節點2.2",
"id": "8",
"parentId": "6"
},
{
"name": "一級節點3",
"id": "11",
"parentId": "1"
},
{
"name": "二級節點3.1",
"id": "12",
"parentId": "11"
},
{
"name": "三級節點3.1.1",
"id": "13",
"parentId": "12"
},
{
"name": "四級節點3.1.1.1",
"id": "14",
"parentId": "13"
},
{
"name": "五級節點3.1.1.1.1",
"id": "15",
"parentId": "14"
}]
說明:
事實上,使用SQL進行遞迴查詢,返回的就是這種格式的資料,即:按照層級關係排好序展示出來;
如果前端需要的是這種排好序的資料,直接返回即可;
如果需要返回樹形結構的話,見文末推薦。
寫在最後
哪位大佬如若發現文章存在紕漏之處或需要補充更多內容,歡迎留言!!!