到处找树状结构的容器都没找到,TreeMap和TreeSet是什么红-黑树没看明白,没时间仔细研究了,干脆简单写一个吧,反正并发要求不是很高,也不怎么排序,就是内存缓存用。代码如下:
/** * 树操作类,定义树型数据结构的常用操作,其中包括定位树结点,插入树结点 删除树结点,修改树结点等。
注意:树根节点的标志从0开始 */ public class DataTree implements Serializable {
private DataTreeNode rootDataTreeNode = null; //定义树根 private Hashtable
nodeList = new Hashtable();
/** * 设置根节点 * @param nodeID 节点nodeid * @param node
节点数据对象 */ private void setRoot(String nodeID, Object node) {
DataTreeNode treeNode = new
DataTreeNode(); treeNode.setLevel(0); treeNode.setNode(node); treeNode.setNodeID(nodeID); treeNode.setUpNodeID("0"); this.rootDataTreeNode
= treeNode; nodeList.put(nodeID, treeNode); }
/** * 设置根节点 * @param node Object,节点包含数据对象 */ public void
setRoot(Object node) { String nodeID =
DataAccess.getCode(); setRoot(node); } /** * 取得根节点 *
@return 树型节点 */ public DataTreeNode getRoot(){ return
rootDataTreeNode; } /** * 取得节点数据对象 * @return 数据对象
*/ public Object getRootNode(){ return rootDataTreeNode.getNode()
; } /** * 给一个节点添加子节点 * @param supNode 父节点 * @param
node 数据对象 * @return 封装过的树型节点 */ public DataTreeNode
addSubNode(DataTreeNode supNode,Object node){ DataTreeNode newTreeNode =
supNode.addSubNode(node); this.nodeList.put( newTreeNode.getNodeID()
,newTreeNode); return newTreeNode; } /** * 删除一个子节点 *
@param supNode 父节点 * @param subNode 子节点 * @return 成功,失败
*/ public boolean removeSubNode(DataTreeNode supNode,DataTreeNode
subNode){ if(supNode.removeSubNode( subNode)){ this.nodeList.remove(
subNode.getNodeID() ); return true; }else{ return
false; } } /** * 删除一个子节点 * @param supNode 父节点 *
@param orderid 序号,从0开始 * @return 成功,失败 */ public boolean
removeSubNode(DataTreeNode supNode,int orderid){ DataTreeNode subNode =
supNode.getSubNode( orderid); if(supNode.removeSubNode(
orderid)){ this.nodeList.remove( subNode.getNodeID() ); return
true; }else{ return false; } } public static
DataTree creatDataTree(ArrayList nodeList, String
upNodeIDName, String nodeIDName, Object root){ String
getNodeIDMethodName = "get"+nodeIDName; String getUpNodeIDMethodName =
"get"+upNodeIDName; String rootNodeid = getValue(root
,getNodeIDMethodName); String nodeid = ""; String upnodeid =
""; Object[][] nodes = new
Object[nodeList.size()][3];
int count =0; //将arraylist数据存储到两个Hashtable中 for(int
i=0;i<nodeList.size();i++){ Object obj = nodeList.get(i); if (obj
!= null){ nodeid = new
String(getValue(obj,getNodeIDMethodName)); if (nodeid != null
){ upnodeid = new String(getValue(obj
,getUpNodeIDMethodName)); if (upnodeid !=
null){ nodes[count][0] = nodeid; nodes[count][1] =
upnodeid; nodes[count][2] = nodeList.get(i); count
++; } } } } DataTree dataTree = new
DataTree(); dataTree.setRoot(
rootNodeid,root); findChildNodeAndAdd(dataTree.getRoot(),nodes); return
dataTree; } private static void findChildNodeAndAdd(DataTreeNode
treeNode,Object[][] nodes){ String nodeid = treeNode.getNodeID() ; for
(int i=0;i<nodes.length ;i++){ if
(nodeid.equals(nodes[i][1])){ DataTreeNode childTreeNode =
treeNode.addSubNode(
(String)nodes[i][0],nodes[i][2]); findChildNodeAndAdd(childTreeNode,nodes); } } } private
static String getValue(Object obj, String functionName){ return
(String)ObjectTool.invokeMethod(obj,null,null,functionName); } /**
* 树型数据结构定义类。定义树型数据结构的基本单元--树型节点的结构。 * */ public class
DataTreeNode {
private String upNodeID; //父节点ID
private String nodeID; //属性节点标志ID
private int level; //层号
private Object node; //存储用数据对象
private ArrayList subNodes; //所有子节点
/** * 空构造 * */ private DataTreeNode()
{ subNodes = new ArrayList(); }
/** * @return Returns the level. */ public int getLevel()
{ return level; }
/** * @param level * The level to set.
*/ private void setLevel(int level) { this.level = level; }
/** * @return Returns the node. */ public Object getNode()
{ return node; }
/** * @param node * The node to set.
*/ private void setNode(Object node) { this.node = node; }
/** * @return Returns the nodeID. */ public String
getNodeID() { return nodeID; }
/** * @param nodeID * The nodeID to set.
*/ private void setNodeID(String nodeID) { this.nodeID =
nodeID; }
/** * @return Returns the orderid. */ public int
getSubNodeOrderid(DataTreeNode treeNode) { for (int i = 0; i <
subNodes.size(); i++) { if (this.getSubNode(i).equals(treeNode))
{ return i; } } return -1; }
/** * @return Returns the subNode. */ public ArrayList
getSubNodes() { return (ArrayList) subNodes.clone(); }
/** * @param subNode * The subNode to set.
*/ private void setSubNodes(ArrayList subNodes) { this.subNodes =
subNodes; }
/** * @return Returns the subNodeNo. */ private int
getSubNodeNo() { return this.subNodes.size(); }
/** * @return Returns the upNodeID. */ public String
getUpNodeID() { return upNodeID; }
/** * @param upNodeID * The upNodeID to set.
*/ private void setUpNodeID(String upNodeID) { this.upNodeID =
upNodeID; }
/** * 增加一个子节点 * * @param treeNode *
带树结构的节点 */ private void addSubTreeNode(DataTreeNode treeNode)
{ this.subNodes.add(treeNode); }
/** * 增加一个子节点,指定nodeid * * @param nodeID
* 指定的nodeid * @param node * 节点附带数据对象
*/ private DataTreeNode addSubNode(String nodeID, Object node)
{ DataTreeNode treeNode = new
DataTreeNode(); treeNode.setLevel(this.level +
1); treeNode.setNode(node); treeNode.setNodeID(nodeID); //treeNode.setOrderid(
getSubNodeNo()
); treeNode.setUpNodeID(this.nodeID); addSubTreeNode(treeNode); return
treeNode; }
/** * 增加一个子节点,自动分配nodeid * * @param node
* 节点附带数据对象 */ private DataTreeNode addSubNode(Object node)
{ String nodeID = DataAccess.getCode(); return addSubNode(nodeID,
node); }
/** * 按序号取出节点 * * @param orderid *
序号 * @return */ private DataTreeNode getSubNode(int orderid)
{ return (DataTreeNode) this.subNodes.get(orderid); }
private boolean removeSubNode(int orderid) { try
{ subNodes.remove(orderid); return true; } catch (Exception
ex) { return false; } }
private boolean removeSubNode(DataTreeNode treeNode) { return
subNodes.remove(treeNode); }
/* * (non-Javadoc) * * @see
java.lang.Object#equals(java.lang.Object) */ public boolean
equals(Object treeNode) { if (treeNode instanceof DataTreeNode)
{ if (this.nodeID.equals(((DataTreeNode) treeNode).getNodeID()))
{ return true; } else { return false; } }
else { return false; } }
/* * (non-Javadoc) * * @see
java.lang.Object#toString() */ public String toString()
{ return this.nodeID + " " + node.toString(); } }
} |