浅谈list列表转无限级树

来源:互联网 发布:python tolist 编辑:程序博客网 时间:2024/06/06 01:05
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"                            }                        ]                    }                ]            }        ]    }]