Search code examples
javanetbeansgraphnetbeans-8jung

how to use getNeighbors function on DirectedSparseGraph of JUNG?


Is there anyone who can give an example of how to use the getNeighbors function of the DirectedSparseGraph implementation of JUNG (http://jung.sourceforge.net/doc/api/edu/uci/ics/jung/graph/DirectedSparseGraph.html). The following is the explanation of the function, but does not give any example of how to actually use the function to retrieve the neighbouring nodes of a vertex.

public Collection<V> getNeighbors(V vertex)
{
    if (!containsVertex(vertex))
        return null;

    Collection<V> neighbors = new HashSet<V>();
    neighbors.addAll(getPreds_internal(vertex));
    neighbors.addAll(getSuccs_internal(vertex));
    return Collections.unmodifiableCollection(neighbors);
}

Here is what I have tried:

theGraph.getVertices().stream().forEach((v) -> {
    Collection<V> neighbors = theGraph.getNeighbors(v);
});

But immediately NetBeans pointed out that "cannot find symbol V". What class should I import?


Solution

  • V is a generic type specifier for the nodes in the graph. If your nodes are String objects, for example--that is, if the node type for theGraph is String--then you would in this case replace V with String.

    You may want to check out this tutorial on generics: https://docs.oracle.com/javase/tutorial/java/generics/index.html