I'm trying to port this Java tutorial to Xojo. I'm struggling to unpack the Set
function below because, whilst short and elegant, it crams a lot of conversions into a small space and I'm not sure if I'm understanding it correctly. It's difficult as Java is not my primary language and Xojo lacks support for generics:
public interface GraphNode {
String getId();
}
public class Graph<T extends GraphNode> {
private final Set<T> nodes;
private final Map<String, Set<String>> connections;
public T getNode(String id) {
return nodes.stream()
.filter(node -> node.getId().equals(id))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("No node found with ID"));
}
public Set<T> getConnections(T node) {
return connections.get(node.getId()).stream()
.map(this::getNode)
.collect(Collectors.toSet());
}
}
I basically can only figure what is happening up to the .stream()
method call:
- Get the
Id
of the passednode
GraphNode
- Get the
Set<String>
from theconnections
Map
whose key matches the retrievedId
What I don't understand is what is happening here:
.map(this::getNode).collect(Collectors.toSet())
Can someone provide pseudocode to explain this?