public static void getChildrenList(List<JSONObject> list,JSONObject pJo){ List<JSONObject> retList=new ArrayList<JSONObject>(); for(JSONObject jo:list){ if(jo.getString("pid").equals(pJo.getString("id"))){ retList.add(jo); jo.put("children", getChildrenList(list, jo)); } } //return retList; pJo.put("children",retList); }
第一種,方法循環一次,比較耗費內存,不建議使用
public static void main(String[] args) { //拼裝數據 List<JSONObject> list=new ArrayList<JSONObject>(); list.add(JSONObject.parseObject("{id:'1',name:'n1',pid:'-1'}")); list.add(JSONObject.parseObject("{id:'2',name:'n1',pid:'-1'}")); list.add(JSONObject.parseObject("{id:'3',name:'n1',pid:'-1'}")); list.add(JSONObject.parseObject("{id:'1-1',name:'n1-1',pid:'1'}")); list.add(JSONObject.parseObject("{id:'2-1',name:'n2-1',pid:'2'}")); list.add(JSONObject.parseObject("{id:'3-1',name:'n3-1',pid:'3'}")); list.add(JSONObject.parseObject("{id:'1-2',name:'n1-2',pid:'1'}")); list.add(JSONObject.parseObject("{id:'2-2',name:'n2-2',pid:'2'}")); list.add(JSONObject.parseObject("{id:'3-2',name:'n3-2',pid:'3'}")); list.add(JSONObject.parseObject("{id:'1-3',name:'n1-3',pid:'1'}")); list.add(JSONObject.parseObject("{id:'2-3',name:'n2-3',pid:'2'}")); list.add(JSONObject.parseObject("{id:'3-3',name:'n3-3',pid:'3'}")); list.add(JSONObject.parseObject("{id:'1-1-1',name:'n1-1-1',pid:'1-1'}")); list.add(JSONObject.parseObject("{id:'1-1-2',name:'n1-1-2',pid:'1-1'}")); list.add(JSONObject.parseObject("{id:'1-1-3',name:'n1-1-3',pid:'1-1'}")); //遞歸 根節點 JSONObject tJo=JSONObject.parseObject("{id:'-1',name:'根節點'}"); //tJo.put("children",getChildrenList(list, tJo)); System.out.println(tJo.toJSONString()); //一次循環 方法,比較好內存不建議使用 Map<String,List<JSONObject>> map1=new HashMap<String, List<JSONObject>>(); for(JSONObject jo:list){ String pid=jo.getString("pid"); String id=jo.getString("id"); if(!map1.containsKey(pid)){//不存在 map1.put(pid, new ArrayList<JSONObject>()); } map1.get(pid).add(jo); if(!map1.containsKey(id)){ map1.put(id, new ArrayList<JSONObject>()); } jo.put("children", map1.get(id)); } JSONObject tJo2=JSONObject.parseObject("{id:'-1',name:'根節點'}"); tJo2.put("children", map1.get("-1")); System.out.println(tJo2.toJSONString()); }
第二種方法,兩次循環,建議使用,節省內存
public static void main(String[] args) { //拼裝數據 List<JSONObject> list=new ArrayList<JSONObject>(); list.add(JSONObject.parseObject("{id:'1',name:'n1',pid:'-1'}")); list.add(JSONObject.parseObject("{id:'2',name:'n1',pid:'-1'}")); list.add(JSONObject.parseObject("{id:'3',name:'n1',pid:'-1'}")); list.add(JSONObject.parseObject("{id:'1-1',name:'n1-1',pid:'1'}")); list.add(JSONObject.parseObject("{id:'2-1',name:'n2-1',pid:'2'}")); list.add(JSONObject.parseObject("{id:'3-1',name:'n3-1',pid:'3'}")); list.add(JSONObject.parseObject("{id:'1-2',name:'n1-2',pid:'1'}")); list.add(JSONObject.parseObject("{id:'2-2',name:'n2-2',pid:'2'}")); list.add(JSONObject.parseObject("{id:'3-2',name:'n3-2',pid:'3'}")); list.add(JSONObject.parseObject("{id:'1-3',name:'n1-3',pid:'1'}")); list.add(JSONObject.parseObject("{id:'2-3',name:'n2-3',pid:'2'}")); list.add(JSONObject.parseObject("{id:'3-3',name:'n3-3',pid:'3'}")); list.add(JSONObject.parseObject("{id:'1-1-1',name:'n1-1-1',pid:'1-1'}")); list.add(JSONObject.parseObject("{id:'1-1-2',name:'n1-1-2',pid:'1-1'}")); list.add(JSONObject.parseObject("{id:'1-1-3',name:'n1-1-3',pid:'1-1'}")); //遞歸 JSONObject tJo=JSONObject.parseObject("{id:'-1',name:'根節點'}"); //tJo.put("children",getChildrenList(list, tJo)); System.out.println(tJo.toJSONString()); //兩次循環 方法,建議使用 Map<String,List<JSONObject>> map=new HashMap<String, List<JSONObject>>(); for(JSONObject jo:list){ String pid=jo.getString("pid"); if(!map.containsKey(pid)){//不存在 map.put(pid, new ArrayList<JSONObject>()); } map.get(pid).add(jo); } for(JSONObject jo:list){ String id=jo.getString("id"); if(map.containsKey(id)){ jo.put("children", map.get(id)); } } JSONObject tJo1=JSONObject.parseObject("{id:'-1',name:'根節點'}"); tJo1.put("children", map.get("-1")); System.out.println(tJo1.toJSONString()); }