public class Vertex {
public final Map<Vertex, Integer> edges;
public final String name;
public Vertex(String name) {
edges = new HashMap<>();
this.name = name;
}
public Vertex addEdge(Vertex other, int weight) {
edges.put(other, weight);
return this;
}
public static void main(String[] args) {
Vertex start = new Vertex("start");
Vertex otherA = new Vertex("otherA");
Vertex otherB = new Vertex("otherB");
Vertex end = new Vertex("end");
start.addEdge(otherA, 1).addEdge(otherB, 2);
otherA.addEdge(otherB, 3).addEdge(end, 4);
otherB.addEdge(end, 5);
}
}
This would limit you to some degree though because you will have a hard time in changing a graph. You will have an even harder time to go through the graph in a different way than from the implicit starting andto the ending vertex. A class to manage that would become necessary. Anyways, these are all implementation details and depend on your requirements.
I've found this tutorial that has a very similar approach to what you did. Maybe you can find some impressions there as well.