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

Viewed 412

I have a Stream of Streams containing elements (Nodes).

Stream<MyStreams>

MyStreams have the type:

Stream<Node>

So actually Stream of Streams have the type:

Stream<Stream<Node>>

Multiple MyStreams may contain the same Node. I want to know how often a particular Node is contained in the Streams. The output-class Stream could look like this:

public class Output
{
    public int count; //number of appereances of a specific node in all streams
    public int nodeId; // ID of the node

    Output()
    {
     ...
    }

}

The final Output-List may be:

nodeId    |     count
12        |     7
14        |     5
28        |     4
...       |     ...

Thanks a lot!

EDIT:

This is what I got so far:

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


}
2 Answers
Related