Graph<String> (final Map<String, String> graph) { final Map<String, List<String>> out = new HashMap<>(); final Map<String, List<String>> in = new HashMap<>(); for (String s : graph.keySet()) { out.put(s, new ArrayList<>()); in.put(s, new ArrayList<>()); } for (Map.Entry<String, String> entry : graph.entrySet()) { String from = entry.getKey(); for (int i = 0; i < entry.getValue().length(); i++) { String to = String.valueOf(entry.getValue().charAt(i)); out.get(from).add(to); in.get(to).add(from); } } return new Graph<>() { @NotNull @Override public Collection<String> getNodes() { return graph.keySet(); } @NotNull @Override public Iterator<String> getIn(final String n) { return in.get(n).iterator(); } @NotNull @Override public Iterator<String> getOut(String n) { return out.get(n).iterator(); } }; }