简体   繁体   English

来自对象集的流API对象

[英]Stream API object from set of objects

I started learning Stream API, but can't make through this one. 我开始学习Stream API,但无法完成。

I'm implementing graphs, so I have classes like so: 我正在实现图,所以我有这样的类:

Graph -> Node -> Edge 图->节点->边

Nodes in graph are stored in Hashset; 图中的节点存储在哈希集中; Edges in Nodes too. 节点中的边缘。

Here's code: 这是代码:

    Graph g = new Graph("Graph 1");
    g.addNode(new Node("A"));
    g.addNode(new Node("B"));
    g.connectNodes("A","B", true); //nodeA, nodeB, bidirectional

    System.out.println(g);

    g.getNodes().stream().map(Node::getConnections);

I dunno what do next with last line. 我不知道接下来要做什么。 At this moment it returns HashSet of edges. 此时,它返回边缘的HashSet。

I tried 我试过了

    g.getNodes().stream().map(Node::getConnections).map(x->x.getNodeB().getName()).forEach(System.out::println);

to get second connected via edge node name, but IDE don't allow me to do next map. 通过边缘节点名称获得第二个连接,但是IDE不允许我进行下一个映射。

Can you give some hints? 你能给些提示吗?

Serghey建议使用flatMap就像一个魅力。

g.getNodes().stream().map(Node::getConnections).flatMap(x->x.stream()).map(x->x.getNodeB().getName()).forEach(System.out::println);

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

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