I need to split line based on comma and add it to a Map simultaneously in one go. I know that I can collect the splitted stream to list and then iterate over list for same effect. But I need to know if that can be done.
Input : List<String> - (A, B) (A,C)
Output : Map<String, List<String>> - (A, (B,C))
Example Code : (Contains Error)
public void edgeListRepresentation(ArrayList<String> edges){
Map<String, List<String>> edgeList = new HashMap<>();
edges.forEach(connection -> {
Stream.of(connection.split(","))
.map((arr) -> {
edgeList.computeIfAbsent(arr[0], val -> new ArrayList<String>()).add(arr[1]);
});
});
}
public static void main(String[] args) throws IOException {
EdgeList edgeList = new EdgeList();
edgeList.edgeListRepresentation(new ArrayList<String>(){
{
add("A, B");
add("A, C");
add("A, E");
add("B, C");
add("C, E");
add("C, D");
add("D, E");
}
});
}