I'm not really happy with my methods to build a tree structure in my J2ME Application. Can anybody point in a more performant direction? If you need some more code to understand my snippets, just comment below. Java Version is 1.4.
Many thanks,
rAyt
if(companyList != null) {
companyList.setNodeStructure(null);
Hashtable nodes = new Hashtable();
for(Enumeration e = companyList.elements(); e.hasMoreElements();) {
Company temp_comp = (Company)e.nextElement();
if(temp_comp.getParentCompanyId() == 0 && temp_comp.getCompanyId() > 0) {
getSubTree(temp_comp.getCompanyId(), companyList, nodes);
}
}
companyList.setNodeStructure(nodes);
Methods
private void getSubTree(int CompanyId, CompanyList _companyList, Hashtable nodes) {
Vector children = getChildren(CompanyId, _companyList);
if(children.size() > 0) {
nodes.put(new Integer(CompanyId), children);
for(Enumeration e = children.elements(); e.hasMoreElements();) {
Company temp_comp = (Company)e.nextElement();
getSubTree(temp_comp.getCompanyId(), _companyList, nodes);
}
}
}
private Vector getChildren(int CompanyId, CompanyList _companyList) {
Vector temp = new Vector();
for(Enumeration e = _companyList.elements(); e.hasMoreElements();) {
Company temp_comp = (Company)e.nextElement();
if(temp_comp.getParentCompanyId() == CompanyId) {
temp.addElement(temp_comp);
}
}
temp.trimToSize();
return temp;
}