将父子关系的java arrayList转换为树?

将父子关系的java arrayList转换为树?,第1张

将父/子关系的java arrayList转换为树?

这是基于第一个答案和问题的更新的替代解决方案… :)

主要方法
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class Main2 {    public static void main(String[] args) {        // input        ArrayList<Pair> pairs = new ArrayList<Pair>();        pairs.add(new Pair( "H" , "G"));        pairs.add(new Pair( "F" , "G"));        pairs.add(new Pair( "G" , "D"));        // ...        // Arrange        // String corresponds to the Id        Map<String, MegaMenuDTO> hm = new HashMap<>();        // you are using MegaMenuDTO as linked list with next and before link        // populate a Map        for(Pair p:pairs){ //  ----- Child ----- MegaMenuDTO mmdChild ; if(hm.containsKey(p.getChildId())){     mmdChild = hm.get(p.getChildId()); } else{     mmdChild = new MegaMenuDTO();     hm.put(p.getChildId(),mmdChild); } mmdChild.setId(p.getChildId()); mmdChild.setParentId(p.getParentId()); // no need to set ChildrenItems list because the constructor created a new empty list // ------ Parent ---- MegaMenuDTO mmdParent ; if(hm.containsKey(p.getParentId())){     mmdParent = hm.get(p.getParentId()); } else{     mmdParent = new MegaMenuDTO();     hm.put(p.getParentId(),mmdParent); } mmdParent.setId(p.getParentId()); mmdParent.setParentId("null");         mmdParent.addChildrenItem(mmdChild);        }        // Get the root        List<MegaMenuDTO> DX = new ArrayList<MegaMenuDTO>();         for(MegaMenuDTO mmd : hm.values()){ if(mmd.getParentId().equals("null"))     DX.add(mmd);        }        // Print         for(MegaMenuDTO mmd: DX){ System.out.println("DX contains "+DX.size()+" that are : "+ mmd);        }    }}
双人课:
public class Pair {    private String childId ;    private String parentId;    public Pair(String childId, String parentId) {        this.childId = childId;        this.parentId = parentId;    }    public String getChildId() {        return childId;    }    public void setChildId(String childId) {        this.childId = childId;    }    public String getParentId() {        return parentId;    }    public void setParentId(String parentId) {        this.parentId = parentId;    }}
MegaMenuDTO类已更新
import java.util.ArrayList;import java.util.List;public class MegaMenuDTO {    private String Id;    private String name;    private String parentId;    private List<MegaMenuDTO> childrenItems;    public MegaMenuDTO() {        this.Id = "";        this.name = "";  this.parentId = "";        this.childrenItems = new ArrayList<MegaMenuDTO>();    }    public String getId() {        return Id;    }    public void setId(String id) {        Id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getParentId() {        return parentId;    }    public void setParentId(String parentId) {        this.parentId = parentId;    }    public List<MegaMenuDTO> getChildrenItems() {        return childrenItems;    }    public void setChildrenItems(List<MegaMenuDTO> childrenItems) {        this.childrenItems = childrenItems;    }    public void addChildrenItem(MegaMenuDTO childrenItem){        if(!this.childrenItems.contains(childrenItem)) this.childrenItems.add(childrenItem);    }    @Override    public String toString() {        return "MegaMenuDTO [Id=" + Id + ", name=" + name + ", parentId="     + parentId + ", childrenItems=" + childrenItems + "]";    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5561132.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存