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
| @Test void testBuildTree(){
List<DivEntity> treeNodes = new ArrayList<>();
treeNodes.add(new DivEntity("2", "0", "陕西省")); treeNodes.add(new DivEntity("21", "2", "西安市")); treeNodes.add(new DivEntity("211", "21", "雁塔区")); treeNodes.add(new DivEntity("212", "21", "未央区")); treeNodes.add(new DivEntity("2121", "212", "浐灞"));
treeNodes.add(new DivEntity("11", "1", "成都市"));
int[] treeNodeFlag = new int[treeNodes.size()];
for (int i = 0; i < treeNodes.size(); i++) { DivEntity treeNode = treeNodes.get(i); if(treeNode.getDivParentCode() != null && !treeNode.getDivParentCode().equals("0")){ for (DivEntity parentNode : treeNodes) { if(treeNode.getDivParentCode().equals(parentNode.getDivCode())){ if(parentNode.getChildren() == null){ parentNode.setChildren(new ArrayList<>()); } parentNode.getChildren().add(treeNode); treeNodeFlag[i] = 1; } } } }
for (int i = 0; i < treeNodeFlag.length; i++) { if(treeNodeFlag[i] == 1){ treeNodes.set(i, new DivEntity("-9999", "-9999", "")); } } treeNodes.removeIf(treeNode ->treeNode.getDivCode().equals("-9999") && treeNode.getDivParentCode().equals("-9999"));
System.out.println("JSON.toJSONString(treeNodes) = " + JSON.toJSONString(treeNodes)); }
|