title: Java: JGraphT: Iterate through nodes
body: <p>I'm trying to iterate through all nodes, so I can print them out for graphviz. What is the best way to do that using the JGraphT library?</p>
  
  <pre><code>public static void main(String[] args) {
      UndirectedGraph&lt;String, DefaultEdge&gt; g = new SimpleWeightedGraph&lt;String, DefaultEdge&gt;(DefaultEdge.class);
  
      String odp = "ODP";
      String cck = "CCK";
      String mfe = "MFE";
  
      g.addVertex(odp);
      g.addVertex(cck);
      g.addVertex(mfe);
  
      g.addEdge(odp, cck);
      g.addEdge(odp, mfe);
  
  }
  </code></pre>
  
  <p>Also, how do I add edge weights?</p>
  
  <p><strong>Edit:</strong> This seems to work pretty well. But is there a better way?</p>
  
  <pre><code>    Set&lt;DefaultEdge&gt; edges = g.edgeSet();
  
      for (DefaultEdge e : edges) {
          gv.addln(String.format("\"%s\" -&gt; \"%s\"", g.getEdgeSource(e), g.getEdgeTarget(e)));            
      }
  </code></pre>