2016-09-29 54 views
3

我是新的集合。 我具有以下從N個關卡中取回所有孩子從集合

class ParantCategory { 
    int id; 
    String name; 
    int pid; 
    public ParantCategory(int id, String name, int pid) { 
     this.id = id; 
     this.name = name; 
     this.pid = pid; 
    } 
    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public int getPid() { 
     return pid; 
    } 
    public void setPid(int pid) { 
     this.pid = pid; 
    } 
} 

和我的主要方法是類如

public class HierachiDemo { 
    static ArrayList<ParantCategory> al = new ArrayList<>(); 
    public static void main(String[] args) { 
     al.add(new ParantCategory(1, "000", 0)); 
     al.add(new ParantCategory(2, "A1", 1)); 
     al.add(new ParantCategory(3, "B1", 1)); 
     al.add(new ParantCategory(4, "A11", 2)); 
     al.add(new ParantCategory(5, "A12", 2)); 
     al.add(new ParantCategory(6, "A13", 2)); 
     al.add(new ParantCategory(7, "B11", 3)); 
     al.add(new ParantCategory(8, "B12", 3)); 
     al.add(new ParantCategory(9, "B13", 3)); 
     al.add(new ParantCategory(10, "A111", 4)); 
     al.add(new ParantCategory(11, "A112", 4)); 
     al.add(new ParantCategory(12, "A113", 4)); 
     HierachiDemo h = new HierachiDemo(); 
     //function call here 
    } 
} 

我已存儲在數組列表元素。 現在我的問題是我需要遞歸函數,我會通過名稱,它會給我所有的孩子從n個層次。 例如: 如果我通過A1的名字,那麼它應該返回我的所有孩子們喜歡的A11,A12,A13,並且如果有subchild的A11,A12,A13也應該顯示。 我需要最終結果爲A11,A12,A13,A111,A112,A113。

+0

是父母和孩子通過ID和PID關係嗎? – matt

+0

你爲什麼試圖在列表(平面)中表示層次結構? – Spotted

回答

3

一個簡單的「蠻力」的解決辦法是:

List<ParantCategory> matches = new ArrayList<>(); 
for (ParantCatagory parant : al) { 
    if (parant.getName().startsWith("A1")) { 
    matches.add(al); 
    } 
} 

您還可以使用Java8用一個簡單的過濾器,而不是流。

當然,這適用於這個簡單的字符串比較。這要澄清的關鍵是什麼這樣「子」的關係實際上意思給你。在你的榜樣,你只需告訴我們您認爲「A112」是一個孩子「A1」的

所以,解決您的評論:如果你想實現真正的層次排序,那麼你必須在代碼表達說。含義:比你可能必須創建自己的的實現;在您添加對象的位置,並且您編寫的代碼爲的代碼爲時,您的樹將這些「子類」類別中的元素排序。

換句話說:你目前的代碼使上串簡單的假設。如果你需要更復雜的東西;那麼,必須執行。你必須採取的第一步:自己澄清這種關係是如何定義的。這不是我們可以幫助的!

換句話說:現在,你只能有一個Parant類的對象。而那些對象絕對是沒有彼此之間的關係。如果你想要這樣的關係,你必須添加一些手段來表達他們!例如通過將您的類更改爲「Node」類;和一個節點...有方法來添加/查詢子節點節點!

+0

感謝您的回覆。 但假設A1的孩子並非以「A1」開頭,那我該怎麼辦? –

+0

我更新了我的答案;但請理解,我們確實可以提供的幫助並不太多。 – GhostCat

+2

我想你使用這個名字來創建一個父子關係,但是OP使用的是id/pid。 id/pid巧合地對應於本例中的名稱。 – matt

1

如果你想堅持當前的結構。你可能會做一個你想要的方法。有兩件事情,

List<ParantCategory> getChildNodes(String name, int levels){ 
    List<ParantCategory> results = new ArrayList<>(); 
    ParantCategory head = null; 

    //assuming there is only one ParantCategory with the supplied name. 
    for(ParantCategory p: al){ 
     if(name.equals(al.getName())){ 
      head = al; 
      break; 
     } 
    } 

    //if we cannot find the name just use an empty list. 
    if(head==null) return results; 
    results.add(head); 
    results.addAll(getChildren(head.getPid(), levels)); 
    return results; 

}

我們可以做一個遞歸方法得到孩子。

void getChildren(int pid, levels){ 

    if(levels==0) return Collection.emptyList(); 

    List<ParantCategory> results = al.stream().filter(
      p->p.getPid()==pid 
     ).collect(Collectors.toList()); 
    for(ParantCategory p: results){ 
     results.addAll(getChildren(p.getPid(), levels-1)); 
    } 
    return results; 
}