繁体   English   中英

如何在Java中比较和计算多个Streams的元素?

[英]How to compare and count elements of multiple Streams in Java?

我有一个包含元素(节点)的流媒体流。

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


}
 List<Output> output = Stream.of(Stream.of(new Node(1), new Node(2)), Stream.of(new Node(2)))
            .flatMap(x -> x)
            .collect(Collectors.collectingAndThen(
                    Collectors.groupingBy(
                            Node::getNodeId,
                            Collectors.summingInt(n -> 1)),
                    map -> map.entrySet()
                            .stream()
                            .map(e -> new Output(e.getKey(), e.getValue()))
                            .collect(Collectors.toList())));

    System.out.println(output); // [id = 1 count = 1, id = 2 count = 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()));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM