import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
public class Graph {
private static Multimap<Integer,Integer> adjacentDirectedNodesMap = ArrayListMultimap.create();
private static Set<Integer> alreadyVisited = new HashSet();
static{
adjacentDirectedNodesMap.put(1, 2);
adjacentDirectedNodesMap.put(1, 3);
adjacentDirectedNodesMap.put(1, 5);
adjacentDirectedNodesMap.put(2, 4);
adjacentDirectedNodesMap.put(4, 5);
}
public static void main(String[] args){
ArrayList visited = new ArrayList();
Integer startNode = 1;
displayAdjacentNodes(startNode);
}
private static void displayAdjacentNodes(Integer integer){
System.out.println(integer);
for(Map.Entry<Integer, Collection<Integer>> adjacentNodes: adjacentDirectedNodesMap.asMap().entrySet()){
for(Integer integer1:adjacentNodes.getValue()){
if(alreadyVisited.contains(integer1)){
continue;
}
alreadyVisited.add(integer1);
System.out.println(integer1);
}
}
}
}
|