我有一个包含元素(节点)的Streams流。
Stream<MyStreams>
MyStreams的类型为:
Stream<Node>
因此实际上Stream of Streams的类型为:
Stream<Stream<Node>>
多个MyStream可能包含相同的节点。我想知道Streams中包含特定节点的频率。输出类Stream可能如下所示:
public class Output
{
public int count; //number of appereances of a specific node in all streams
public int nodeId; // ID of the node
Output()
{
...
}
}
最终的输出列表可能是:
nodeId | count
12 | 7
14 | 5
28 | 4
... | ...
非常感谢!
修改
这是我到目前为止所得到的:
public class Correlation {
@Context
public GraphDatabaseService db;
@Context
public Log log;
@Procedure
@Description("xxx")
// Main
public Stream<NeighborNodeStream> analysisNeighbors( @Name("label") String labelString,
@Name("key") String key,
@Name("value") String value)
{
Label label = Label.label(labelString);
// Get Stream of Starting Nodes
Stream<Node> myNodes = db.findNodes(label, key, value).stream();
// For every Stating node get Stream of Neighbor nodes
Stream<NeighborNodeStream> myNeighborNodeStreams = myNodes.map(x -> new NeighborNodeStream(x));
// ***Nodes count ***
return myNeighborNodeStreams;
}
public class NeighborNodeStream
{
public Stream<Node> neighborNodes;
public NeighborNodeStream(Node node)
{
Stream<Relationship> relsStream = StreamSupport.stream(node.getRelationships().spliterator(), false);
this.neighborNodes = relsStream.map(x -> getRightNode(x, node.getId()));
}
}
private Node getRightNode(Relationship rel, long nodeId)
{
long endId = rel.getEndNode().getId();
if (endId == nodeId)
return rel.getStartNode();
else
return rel.getEndNode();
}
}
答案 0 :(得分:3)
TABLE(...)
答案 1 :(得分:2)
如果我正确理解OP,那应该是一个简单的逐句问题:
Stream<NeighborNodeStream> nodess = ...;
// key is the Node id, value is the occurrences:
Map<Long, Long> res = nodess.flatMap(s -> s.neighborNodes)
.collect(Collectors.groupingBy(n -> n.getId(), Collectors.counting()));