java 链接 递归 如何转成树 java递归生成树_List


上面是基本的树结构

下面是java代码生成这样的树的json代码

这里不包括展现,展现很简单

/***
	 * pid 父id
	 * allList 所有数据库查出来的list数据
	 */
	public List<Map<String,Object>> getChild(String pid , List<ModelStruc> allList){
	     //   List<UserMenu> childList = new LinkedList<>();
	        List<Map<String,Object>> childList = new ArrayList<>();//用于保存子节点的list
	        for(ModelStruc ms : allList){
	            if(pid.equals(ms.getPid())){//判断传入的父id是否等于自身的,如果是,就说明自己是子节点
	            //    childList.add(userMenu);
	                Map<String,Object> map = new HashMap<>();
	                map.put("id",ms.getId());
	                map.put("pId", ms.getPid());
	                map.put("name",ms.getName());
	              //  map.put("value",userMenu.getId());
	             //   map.put("showcheck",true);
	             //   map.put("checkstate",0);
	                map.put("hasChildren",false);
	              //  map.put("isexpand",true);
	              //  map.put("complete",true);
	                map.put("ChildNodes", new Object[]{});
	                childList.add(map); //加入子节点
	            }
	        }
	        for(Map<String,Object> map : childList){//遍历子节点,继续递归判断每个子节点是否还含有子节点
	            List<Map<String,Object>> tList = getChild(String.valueOf(map.get("id"))  , allList);
	            if(!tList.isEmpty()){
	                map.put("hasChildren",true);
	            }
	            map.put("ChildNodes" , tList);
	           // userMenu.setChildList(getChild(userMenu.getResourceCode() , allList));
	        }
	        return childList;
	    }

使用方式:

List<ModelStruc> list = dao.selectByCode2(modelCode,version);//
		List list2 = modelStrucService.getChild("null", list);//默认根节点pid是null
		String json = JSON.toJSONString(list2);
		System.out.println(json);

运行打印的结果:

[{
	"ChildNodes": [{
		"ChildNodes": [],
		"hasChildren": false,
		"name": "2",
		"pId": "1dffsdad",
		"id": "424636cf08c444db872b2d7cb313b796"
	}, {
		"ChildNodes": [{
			"ChildNodes": [],
			"hasChildren": false,
			"name": "4",
			"pId": "7d1f42499bc34f69a6c659bae64097ab",
			"id": "00fa5469a1a44a6e963a288340ce93ca"
		}, {
			"ChildNodes": [{
				"ChildNodes": [],
				"hasChildren": false,
				"name": "ne2",
				"pId": "e4b870a1e927466ea1d53c8d333563a1",
				"id": "79f06788494c4336a95558f44241224a"
			}, {
				"ChildNodes": [],
				"hasChildren": false,
				"name": "ne",
				"pId": "e4b870a1e927466ea1d53c8d333563a1",
				"id": "9a97ec2787cd4ec99018a29f65ec50bb"
			}],
			"hasChildren": true,
			"name": "5",
			"pId": "7d1f42499bc34f69a6c659bae64097ab",
			"id": "e4b870a1e927466ea1d53c8d333563a1"
		}],
		"hasChildren": true,
		"name": "3",
		"pId": "1dffsdad",
		"id": "7d1f42499bc34f69a6c659bae64097ab"
	}, {
		"ChildNodes": [],
		"hasChildren": false,
		"name": "1",
		"pId": "1dffsdad",
		"id": "7ea8863043a244ffb7986877ae7a1960"
	}],
	"hasChildren": true,
	"name": "POS模型",
	"pId": "null",
	"id": "1dffsdad"
}]

表中的数据:

java 链接 递归 如何转成树 java递归生成树_树_02


ModelStruc部分数据

public class ModelStruc implements java.io.Serializable {
	/**ID*/
	private java.lang.String id;
	/**名称*/
	
	private java.lang.String name;
	/**排序*/
	private java.lang.String morder;
	/**父菜单ID*/
	private java.lang.String pid;
	/**图标ID*/
	private java.lang.String iconid;
	/**创建人id*/

	private java.lang.String createBy;
	/**创建人*/

	private java.lang.String createName;
	/**修改人id*/
	private java.lang.String updateBy;
	/**修改时间*/
	private java.util.Date updateDate;
	/**创建时间*/
	private java.util.Date createDate;
	/**修改人*/
	private java.lang.String updateName;
	/**模型代码*/
	private java.lang.String icode;
	
	private java.lang.String version;
	
	private List<ModelStruc> children;
	}