如今有一個樹形結構的元素集合map list,要求遍歷該樹的全部分支java
代碼以下:web
package com.inslink.controller.element; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.baomidou.mybatisplus.service.IService; import com.inslink.controller.GenericController; import com.inslink.model.element.core.Element; import com.inslink.service.element.ElementService; public class RobotLogicTreeController { @Autowired private ElementService elementService; private static boolean flag = true; private static List<List<Map<String, Object>>> excellist = new ArrayList<List<Map<String,Object>>>(); private static Map<String, Object> parentMap = new HashMap<String, Object>(); private static List<Map<String,Object>> mlist = new ArrayList<Map<String,Object>>(); //遍歷全部分支 public static List<Map<String,Object>> getSonMap(Map<String,Object> parentMap1,List<Map<String,Object>> treeElements,List<Map<String,Object>> list) { if (parentMap1.get("parent")==null) { list.add(parentMap1); } for (int i=0;i<treeElements.size();i++) { Map<String,Object> map = treeElements.get(i); if (map.get("parent")!=null && parentMap1.get("id").toString().equals(map.get("parent").toString())) { flag = true; list.add(map); getSonMap(map,treeElements,list); } //不存在子節點 else { flag = false; continue; } if (!flag) { if (list.size()>1) { //判斷新增的list中的在樹中是否存在子節點 boolean exist = false; for (Map<String,Object> s : mlist) { System.out.println(s.get("parent")); if (s.get("parent")!=null && s.get("parent").toString().equals(map.get("id").toString())) { exist = true; break; } } if (!exist) { excellist.add(list); } } //刪除此分支上全部只有一個子節點的節點或末支節點 treeElements.remove(map); list = new ArrayList<>(); getSonMap(parentMap,treeElements,list); break; } } return list; } @Override protected IService<Element> getService() { // TODO Auto-generated method stub return null; } public static void main(String[] args) { Map<String,Object> map1 = new HashMap<String,Object>(); map1.put("name", "乳腺"); map1.put("level", "0"); map1.put("id", "1"); map1.put("parent", null); Map<String,Object> map2 = new HashMap<String,Object>(); map2.put("name", "乳腺1"); map2.put("level", "1"); map2.put("id", "2"); map2.put("parent", "1"); Map<String,Object> map3 = new HashMap<String,Object>(); map3.put("name", "乳腺2-1"); map3.put("level", "2"); map3.put("id", "3"); map3.put("parent", "2"); Map<String,Object> map4 = new HashMap<String,Object>(); map4.put("name", "乳腺2-2"); map4.put("level", "2"); map4.put("id", "4"); map4.put("parent", "2"); Map<String,Object> map5 = new HashMap<String,Object>(); map5.put("name", "乳腺2-3"); map5.put("level", "2"); map5.put("id", "5"); map5.put("parent", "2"); Map<String,Object> map6 = new HashMap<String,Object>(); map6.put("name", "乳腺3-1"); map6.put("level", "3"); map6.put("id", "6"); map6.put("parent", "3"); Map<String,Object> map8 = new HashMap<String,Object>(); map8.put("name", "乳腺3-8"); map8.put("level", "3"); map8.put("id", "8"); map8.put("parent", "3"); Map<String,Object> map7 = new HashMap<String,Object>(); map7.put("name", "乳腺3-2"); map7.put("level", "3"); map7.put("id", "7"); map7.put("parent", "5"); List<Map<String,Object>> treeElements = new ArrayList<Map<String,Object>>(); treeElements.add(map1); treeElements.add(map2); treeElements.add(map3); treeElements.add(map4); treeElements.add(map5); treeElements.add(map6); treeElements.add(map7); treeElements.add(map8); // Map<String,Object> treeMap = new HashMap<String,Object>(); // map = sortTreeMap(map1, treeElements,treeMap); mlist = new ArrayList<Map<String,Object>>(); mlist = new ArrayList<Map<String,Object>>((Collection<? extends Map<String, Object>>) Arrays.asList(new Map[treeElements.size()])); Collections.copy(mlist, treeElements); parentMap = map1; List<Map<String,Object>> list = new ArrayList<Map<String,Object>>(); list = getSonMap(parentMap, treeElements,list); for ( List<Map<String, Object>> slist : excellist) { System.out.println("====="); for (Map<String,Object> smap : slist) { System.out.println(smap.get("id")); } } } }