Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>getChildren() could be much more efficient. It looks like, every time you call it, you're iterating through the entire CompanyList to find the ones whose parent matches the given CompanyId. That just screams for a Hashtable filled with Vectors. The Hashtable keys would be the parent ID, not the original ID; the values would be vectors that contain the company's whose parent ID matches the given parent ID. Then you have:</p> <pre><code>private Vector getChildren(int CompanyId, Hashtable companyParentLoookup) { return (Vector) companyParentLookup.get(CompanyId); } </code></pre> <p>Of course, it looks like your objective in writing getSubTree is actually to <em>construct</em> the Hashtable I just described. The problem here is that you're trying to construct it company-ID by company-ID, rather than Company by Company. You could try this instead, to construct the companyParentLookup:</p> <pre><code>private Hashtable calcLookupTable(CompanyList _companyList) { Hashtable retval = new Hashtable(); for (Enumeration e = _companyList.elements(); e.hasMoreElements();) { Company temp_comp = (Company) e.nextElement(); Integer parent_id = temp_comp.getParentCompanyID(); if (retval.containsKey(parent_id) == false) { retval.put(parent_id, new Vector()); } retval.get(parent_id).add(temp_comp); } return retval; } </code></pre> <p>You can then stick the companyParentLookup structure directly into companyList's nodeStructure.</p> <p>EDIT: The relevant point here is that you can lazy-initialize the Hashtable's vector entries as you need them; each time you can just check if the Hashtable has the needed entry, and if it doesn't, you just throw it in. This is what allows you to create the Hashtable by iterating on the companies as children rather than iterating on them as parents. If you really can't use anything but Hashtables and Vectors, then this is not a bad way to implement a tree; it's roughly equivalent to keeping a graph data structure using adjacency lists. I actually wrote something that looks very much like this for a graph data structure, albeit with HashMaps.</p> <p>Er, does that help?</p>
 

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