查找许多列表之间的依赖关系

时间:2019-03-21 13:02:09

标签: java

我有一个整数列表List<List<Integer>> dependency = new ArrayList<List<Integer>>()的列表,其中包含以下列表:

[0, 0]
[1, 0]
[2, 1]
[3, 1]
[4, 3]
[5, 1]
[6, 4]
[8, 5]
[9, 3]
[10, 2]
[11, 6]

每个列表都包含ID及其与另一个列表的依赖关系。例如,可以读取[2,1],因为ID为2取决于ID = 1([1,0])的列表。

我想知道列表之间的依赖关系,类似于以下示例:

10 <-2 <-1 <-0(这可以理解为:ID列表= 10取决于ID列表= 2取决于ID列表= 1取决于ID列表= 0)< / p>

8 <-2 <-1 <-0(ID列表= 8取决于ID = 5列表,ID取决于1 = 1列表取决于ID = 0列表)

为此,我执行了以下操作:

    for(List<Integer> x: dependency) {
        x1 = x.get(0);
        x2 = x.get(1);

        int x3 = dependency.get(x2).get(1);
        System.out.println(x1 + " -- " + x2 + " -- " + x3);
    }

但这不适用于ID = 6的情况,因为它具有更多的依赖性: 6 <-4 <-3 <-1 <-0

另外,对于11: 11 <-6 <-4 <-3 <-1 <-0

如何解决此问题,以便我可以获得尽可能多的依赖关系?

2 个答案:

答案 0 :(得分:3)

在我看来,这是经典的递归用法。 我曾经遵循以下代码:

import java.util.ArrayList;
import java.util.List;

public class Listofintegerlists {

    public static void init(List<List<Integer>> list) {
    ...
    }

    public static void main(String[] args) {
        List<List<Integer>> dependency = new ArrayList<List<Integer>>();
        init(dependency);
        System.out.println(dependency);
        printDependenciesAux(dependency);
    }

    public static void printDependenciesAux(List<List<Integer>> dependency) {
        for (List<Integer> x : dependency) {
            System.out.print("Printing dependencies for ID = " + x.get(0) + ": ");
            printDepedencies(x, dependency);
            System.out.println();
        }
    }
    public static void printDepedencies(List<Integer> curList, List<List<Integer>> dependency) {
        int curID = curList.get(0);
        int nextID = curList.get(1);

        System.out.print(curID + " <-- ");
        if (nextID == 0) {
            System.out.print("0");
            return;
        }
        List<Integer> nextList = getNextList(nextID, dependency);
        printDepedencies(nextList, dependency);
    }

    private static List<Integer> getNextList(int nextID, List<List<Integer>> dependency) {
        for (List<Integer> x : dependency) {
            if (x.get(0) == nextID) {
                return x;
            }
        }
        return null;
    }
}

输出:

[[0, 0], [1, 0], [2, 1], [3, 1], [4, 3], [5, 1], [6, 4], [8, 5], [9, 3], [10, 2], [11, 6]]
Printing dependencies for ID = 0: 0 <-- 0
Printing dependencies for ID = 1: 1 <-- 0
Printing dependencies for ID = 2: 2 <-- 1 <-- 0
Printing dependencies for ID = 3: 3 <-- 1 <-- 0
Printing dependencies for ID = 4: 4 <-- 3 <-- 1 <-- 0
Printing dependencies for ID = 5: 5 <-- 1 <-- 0
Printing dependencies for ID = 6: 6 <-- 4 <-- 3 <-- 1 <-- 0
Printing dependencies for ID = 8: 8 <-- 5 <-- 1 <-- 0
Printing dependencies for ID = 9: 9 <-- 3 <-- 1 <-- 0
Printing dependencies for ID = 10: 10 <-- 2 <-- 1 <-- 0
Printing dependencies for ID = 11: 11 <-- 6 <-- 4 <-- 3 <-- 1 <-- 0

答案 1 :(得分:1)

假设不存在像[1,2]和[2,1]这样的相互依赖项,则可以选择创建地图以递归方式导航:

public static void main(String[] args)  {
    List<List<Integer>> dependency = new ArrayList<>(); // fill your list
    //create a map
    Map<Integer,Integer> depMap = dependency.stream().collect(Collectors.toMap(k->k.get(0), k->k.get(1)));
    //print dependencies using the recursive method provided below 
    System.out.println(getdependencies(depMap,10)); 
}

public static String getdependencies(Map<Integer,Integer> map, Integer key) {
    if(map.containsKey(key)){
        if (map.get(key) == 0) {
            return key + " <- " + map.get(key).toString();
        }
        return key + " <- " + getdependencies(map, map.get(key));
    }
    return "Key: "+key+" not found";
}