如何在 JUNG 的 DirectedSparseGraph 上使用 getNeighbors 函数?



有没有人可以举个例子来说明如何使用JUNG(http://jung.sourceforge.net/doc/api/edu/uci/ics/jung/graph/DirectedSparseGraph.html(的DirectedSparseGraph实现的getNeighbors函数。以下是该函数的说明,但没有给出任何如何实际使用该函数检索顶点的相邻节点的示例。

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);
}

这是我尝试过的:

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

但NetBeans立即指出"找不到符号V"。我应该导入什么类?

V是图中节点的泛型类型说明符。 例如,如果您的节点是String对象(即,如果theGraph的节点类型是String(,那么在这种情况下,您将V替换为 String。

您可能想查看有关泛型的本教程:https://docs.oracle.com/javase/tutorial/java/generics/index.html

最新更新