Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I work with graph structures in Java a lot, and my advice would be to make any data member of the Node and Edge class that the Graph depends on for maintaining its structure final, with no setters. In fact, if you can, I would make Node and Edge completely immutable, which has <a href="http://www.javapractices.com/topic/TopicAction.do?Id=29" rel="nofollow noreferrer">many benefits</a>.</p> <p>So, for example:</p> <pre><code>public final class Node { private final String name; public Node(String name) { this.name = name; } public String getName() { return name; } // note: no setter for name } </code></pre> <p>You would then do your uniqueness check in the Graph object:</p> <pre><code>public class Graph { Set&lt;Node&gt; nodes = new HashSet&lt;Node&gt;(); public void addNode(Node n) { // note: this assumes you've properly overridden // equals and hashCode in Node to make Nodes with the // same name .equal() and hash to the same value. if(nodes.contains(n)) { throw new IllegalArgumentException("Already in graph: " + node); } nodes.add(n); } } </code></pre> <p>If you need to modify a name of a node, remove the old node and add a new one. This might sound like extra work, but it saves a lot of effort keeping everything straight.</p> <p>Really, though, creating your own Graph structure from the ground up is probably unnecessary -- this issue is only the first of many you are likely to run into if you build your own.</p> <p>I would recommend finding a good open source Java graph library, and using that instead. Depending on what you are doing, there are a few options out there. I have used <a href="http://jung.sourceforge.net/" rel="nofollow noreferrer">JUNG</a> in the past, and would recommend it as a good starting point.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload