浅谈list列表转无限级树

class MtreeNode{
	private String id;
	private String value;
	private String text;
	private String pid;
	private List<MtreeNode> children;
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public String getPid() {
		return pid;
	}
	public void setPid(String pid) {
		this.pid = pid;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public List<MtreeNode> getChildren() {
		return children;
	}
	public void setChildren(List<MtreeNode> children) {
		this.children = children;
	}
	
}


public static void main(String[] args) {
		List<MtreeNode> list = new ArrayList<MtreeNode>();
		MtreeNode node1 = new MtreeNode();
		node1.setId("001");node1.setPid(null);node1.setText("root");node1.setValue("root");
		MtreeNode node2 = new MtreeNode();
		node2.setId("002");node2.setPid("001");node2.setText("child1");node2.setValue("child1");
		MtreeNode node3 = new MtreeNode();
		node3.setId("003");node3.setPid("002");node3.setText("child2");node3.setValue("child2");
		MtreeNode node4 = new MtreeNode();
		node4.setId("004");node4.setPid("003");node4.setText("child3");node4.setValue("child3");
		list.add(node1);list.add(node2);list.add(node3);list.add(node4);
		System.out.println(JsonUtils.toJson(list));
		Map<String, MtreeNode> map = new HashMap<String,MtreeNode>();
		List<MtreeNode> result = new ArrayList<MtreeNode>();
		for(MtreeNode temp:list){
			map.put(temp.getId(), temp);
		}
		for(MtreeNode temp:list){
			if(StringUtils.isBlank(temp.getPid())){
				result.add(temp);
			}else{
				if(CollectionUtils.isEmpty(map.get(temp.getPid()).getChildren())){
					List<MtreeNode> childs = new ArrayList<MtreeNode>();
					childs.add(temp);
					map.get(temp.getPid()).setChildren(childs);
				}else{
					map.get(temp.getPid()).getChildren().add(temp);
				}
			}
		}
		Gson gson = new Gson();
		System.out.println(gson.toJson(result));
	}
源数据:
[
    {
        "id": "001",
        "value": "root",
        "text": "root"
    },
    {
        "id": "002",
        "value": "child1",
        "text": "child1",
        "pid": "001"
    },
    {
        "id": "003",
        "value": "child2",
        "text": "child2",
        "pid": "002"
    },
    {
        "id": "004",
        "value": "child3",
        "text": "child3",
        "pid": "003"
    }
]
转换结果:
[
    {
        "id": "001",
        "value": "root",
        "text": "root",
        "children": [
            {
                "id": "002",
                "value": "child1",
                "text": "child1",
                "pid": "001",
                "children": [
                    {
                        "id": "003",
                        "value": "child2",
                        "text": "child2",
                        "pid": "002",
                        "children": [
                            {
                                "id": "004",
                                "value": "child3",
                                "text": "child3",
                                "pid": "003"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值