0
키가 노드 차수이고 값이 해당 차수 값을 가진 모든 노드의 컬렉션 인 해시 맵을 채우려고합니다. 지금은이 코드를 내놓았다 :그래프에서 발견 된 각 학위에 대한 노드 컬렉션을 찾는 방법
// hashmap to hold the result
HashMap<Integer, Collection<Node>> result = new HashMap<Integer, Collection<Node>>();
// for each node in the list
for (Node n : nodes) {
// find node's neighbors
n.setNei(g.getNeighbors(n));
// find node's degree
n.setDegree(n.getNei().size());
// placeholder
Integer degree = n.getDegree();
// if that degree is already present as a key in result
if (result.containsKey(degree)) {
// add n to the list of nodes that has that degree value
boolean add = result.get(degree).add(n);
// check
if (!add) {
// raise exception
throw new ExtensionException("ERROR: failed to add node to list of nodes with degree " + degree);
}
// if that degree is not already present in result
} else {
// create a new empty collection of nodes
List<Node> newList = new ArrayList<Node>();
// add n as the first element in the new collection
boolean add = newList.add(n);
// check
if (add) {
// add degree to the key and the collection of nodes with such degree
result.put(degree, newList);
} else {
// raise exception
throw new ExtensionException("ERROR: failed to add node to list of nodes with degree " + degree);
}
}
}
하지만 JUNG 어떤 클래스보다 효율적으로 내 코드는이 작업을 수행하는 것보다이 있는지 궁금하다. 요점은 학위 분포뿐 아니라 어느 정도 노드 집합을 저장하고 싶다는 것입니다.
어쨌든 나는 내 것보다 더 효율적인 솔루션에 대한 포인터를 고맙게 생각합니다.
안부, 시몬
시간 내 주셔서 대단히 감사합니다! 좋아, 그냥 구아바 멀티 맵을 봤는데 R에서 데이터 프레임처럼 보이는데, 내 키가 행이고 내가 입력 한 항목이 새 열과 같아서 각 키가 내가 매핑하는 모든 노드에 매핑됩니다. 지금 컬렉션 ... 요점을 얻었습니까? –
user299791
그건 일종의 생각입니다. 더 간단하고 더 Java와 비슷한 생각은 Multimap이 Map >의 별칭이라는 것입니다. 필요에 따라 컬렉션을 만드는 등의 작업을 수행합니다. –