1. 程式人生 > 其它 >如何高效地將List轉換成樹形結構,一層迴圈,HashMap實現

如何高效地將List轉換成樹形結構,一層迴圈,HashMap實現

開發過程中經常遇到從List裡面找出父子結構,一個子節點對應一個父節點,一個父節點可以有多個子節點。

比如 南京市、蘇州市都是江蘇省,浙江市、金華市屬於浙江省。

輸入:

[

{fatherId='1', fatherName='江蘇省', sonId='1', sonName='南京市'},

{fatherId='1', fatherName='江蘇省', sonId='2', sonName='蘇州市'},

{fatherId='2', fatherName='浙江省', sonId='3', sonName='杭州市'},

{fatherId='3', fatherName='安徽省', sonId='4', sonName='合肥市'}

]

輸出:

[

{fatherId='1', fatherName='江蘇省', rawDataList=[

{fatherId='1', fatherName='江蘇省', sonId='1', sonName='南京市'},

{fatherId='1', fatherName='江蘇省', sonId='2', sonName='蘇州市'}

]

},

{fatherId='2', fatherName='浙江省', rawDataList=[{fatherId='2', fatherName='浙江省', sonId='3', sonName='杭州市'}]},

{fatherId='3', fatherName='安徽省', rawDataList=[{fatherId='3', fatherName='安徽省', sonId='4', sonName='合肥市'}]}

]

話不多說,上程式碼:

原始資料

public class RawData  {
    private String fatherId;
    private String fatherName;
    private String sonId;
    private String sonName;

    public String getFatherId() {
        return fatherId;
    }

    public void setFatherId(String fatherId) {
        this.fatherId = fatherId;
    }

    
public String getFatherName() { return fatherName; } public void setFatherName(String fatherName) { this.fatherName = fatherName; } public String getSonId() { return sonId; } public void setSonId(String sonId) { this.sonId = sonId; } public String getSonName() { return sonName; } public void setSonName(String sonName) { this.sonName = sonName; } public RawData() { } public RawData(String fatherId, String fatherName, String sonId, String sonName) { this.fatherId = fatherId; this.fatherName = fatherName; this.sonId = sonId; this.sonName = sonName; } @Override public String toString() { return "{" + "fatherId='" + fatherId + '\'' + ", fatherName='" + fatherName + '\'' + ", sonId='" + sonId + '\'' + ", sonName='" + sonName + '\'' + '}'; } }

目的樹形結構

public class Tree {
    private String fatherId;
    private String fatherName;
    private  List<RawData> rawDataList;

    public String getFatherId() {
        return fatherId;
    }

    public void setFatherId(String fatherId) {
        this.fatherId = fatherId;
    }

    public String getFatherName() {
        return fatherName;
    }

    public void setFatherName(String fatherName) {
        this.fatherName = fatherName;
    }

    public List<RawData> getRawDataList() {
        return rawDataList;
    }

    public void setRawDataList(List<RawData> rawDataList) {
        this.rawDataList = rawDataList;
    }

    @Override
    public String toString() {
        return "{" +
                "fatherId='" + fatherId + '\'' +
                ", fatherName='" + fatherName + '\'' +
                ", rawDataList=" + rawDataList +
                '}';
    }
}

 處理邏輯

 

 1 public class Application {
 2     public static void main(String[] args) {
 3         List<RawData> rawDataList = new ArrayList<>();
 4         rawDataList.add(new RawData("1","江蘇省","1","南京市"));
 5         rawDataList.add(new RawData("1","江蘇省","2","蘇州市"));
 6         rawDataList.add(new RawData("2","浙江省","3","杭州市"));
 7         rawDataList.add(new RawData("3","安徽省","4","合肥市"));
 8         System.out.println(rawDataList);
 9         List<Tree> tree = getTree(rawDataList);
10         System.out.println(tree);
11 
12     }
13     public static List<Tree> getTree(List<RawData> rawDataList){
14         List<Tree> treeList = new ArrayList<>();
15         Map<String,List<RawData>> treeMap = new HashMap<>();
16         Set<String> father = treeMap.keySet();
17         for (RawData r:rawDataList) {
18             treeMap.putIfAbsent(r.getFatherId(),new ArrayList<>());
19             treeMap.get(r.getFatherId()).add(r);
20         }
21         for (String fatherId:father) {
22             Tree tree = new Tree();
23             tree.setFatherId(fatherId);
24             tree.setFatherName(treeMap.get(fatherId).get(0).getFatherName());
25             tree.setRawDataList(treeMap.get(fatherId));
26             treeList.add(tree);
27         }
28         return treeList;
29     }
30 }

處理邏輯:

將父節點id 和 原始資料list作為一個map,對原始資料list進行迴圈遍歷,如果原始資料list裡面物件的fatherId沒有出現過,則建立一個新的ArrayList,然後把原始資料物件增加到Map的value裡面。

如果原始資料的list裡面有fatherId那麼把這個物件追加到該fatherId對應的Value裡面。