Java Easy to Understand Topological Sort
```\nclass Solution {\n    public List> getAncestors(int n, int[][] edges) {\n        Map dependencyCountMap = new HashMap<>();\n        Map> nextNodeMap = new 
```\nclass Solution {\n    public List<List<Integer>> getAncestors(int n, int[][] edges) {\n        Map<Integer,Integer> dependencyCountMap = new HashMap<>();\n        Map<Integer, Set<Integer>> nextNodeMap = new HashMap<>();\n        Map<Integer, Set<Integer>> prevNodeMap = new HashMap<>();\n        Queue<Integer> queue = new LinkedList<>();\n        for(int []edge: edges){\n            dependencyCountMap.put(edge[1],dependencyCountMap.getOrDefault(edge[1],0)+1);\n            Set<Integer> set = nextNodeMap.getOrDefault(edge[0],new HashSet<>());\n            set.add(edge[1]);\n            nextNodeMap.put(edge[0], set);\n            \n            set = prevNodeMap.getOrDefault(edge[1],new HashSet<>());\n            set.add(edge[0]);\n            prevNodeMap.put(edge[1], set);\n        }\n        Map<Integer, Set<Integer>> map = new HashMap<>();\n        List<List<Integer>> result = new ArrayList<>();\n        for(int i=0;i<n;i++){\n                if(dependencyCountMap.getOrDefault(i,0)==0){//if there are no unresolved dependencies\n                    queue.offer(i);\n                }\n        }\n        while(queue.size()>0){\n            int current = queue.poll();\n            for(int next: nextNodeMap.getOrDefault(current,new HashSet<>())){\n                int nextCount = dependencyCountMap.getOrDefault(next,0);\n                dependencyCountMap.put(next, nextCount - 1);\n                if(nextCount -1 == 0)\n                    queue.offer(next);\n            }\n            \n            for(int prev: prevNodeMap.getOrDefault(current,new HashSet<>())){//pick resolved dependecies and add to result\n                        map.putIfAbsent(current,new HashSet<>());\n                        map.get(current).add(prev);//add 3,4 for 6\n                        map.get(current).addAll(map.getOrDefault(prev, new HashSet<>()));\n            }\n        }\n        for(int i=0;i<n;i++){\n\t\t\t List<Integer> list =   new ArrayList<>(map.getOrDefault(i, new HashSet<>()));\n             Collections.sort(list);\n\t\t\tresult.add(list);\n        }\n        \n        return result;\n    }\n}
[]