Location>code7788 >text

Tree structure tools

Popularity:684 ℃/2024-09-25 15:51:24
package ; import ; import ; import ; import ; /** * :: Tree-structured tool classes */ public class TreeUtil{ /** * Recursively get all child nodes based on parent id to tree structure * *@param idFieldName id field name *@param pIdFieldName pid field name *@param childrenFieldName children field name *childrenFieldName@param pxFieldName px field name *@param pId parent node id *@param allList List of all menus *@return List of all submenus under each root node */ public static <M> List<M> toTreeByParentId(String idFieldName, String pIdFieldName, String childrenFieldName, String pxFieldName,String pId, List<M> allList){ //child node List<M> childList = new ArrayList<>(()); for (int i = 0; i < (); i++) { M model = (i); //Iterate through all nodes comparing the node's parent id with the id of the root node passed to it //Parent node id field, e.g. pid if ((model,pIdFieldName).equals(pId)){ (model); //Delete to reduce the number of next cycles (i); i--; } } //recursive (calculation) for (M model : childList) { //Primary key field, e.g. id, child node field, e.g. children (model,childrenFieldName,(idFieldName,pIdFieldName,childrenFieldName,pxFieldName,((model,idFieldName)), allList)); } //Sorting fields, e.g. px, can be commented out if no sorting is required if(null != pxFieldName && !()){ ((m -> (((m, pxFieldName))))); //arrange in order } //Assign null values to the children of the bottom node to save memory space if(() <= 0){ childList = null; } return childList; } public static <M> List<M> toTreeByParentId(String pId, List<M> allList){ //Let's set the defaults. return ("id","pid","children","px",pId,allList); } /** * Recursively fetch all parent nodes based on child ids to a tree structure * *@param idFieldName id field name *@param pIdFieldName pid field name *@param childrenFieldName children field name *childrenFieldName@param cId child node id *@param allList List of all menus *@return List of all submenus under each root node */ public static <M> M toTreeByChildrenId(String idFieldName, String pIdFieldName, String childrenFieldName,String cId, List<M> allList){ return (idFieldName,pIdFieldName,childrenFieldName,null,cId,allList); } private static <M> M toTreeByChildrenId(String idFieldName, String pIdFieldName, String childrenFieldName,M parent,String cId, List<M> allList){ //parent node M newParent = null; for (int i = 0; i < (); i++) { M model = (i); // Equivalent description: find the current node if ((model,idFieldName).equals(cId)){ newParent = model; //Setting child nodes if(parent != null){ ArrayList<M> childList = new ArrayList<>(1); (parent); (newParent,childrenFieldName, childList); } //Delete to reduce the number of next cycles (i); i--; break; } } //A null parent node indicates a top-level node String menuParentId = newParent == null ? "" : ((newParent,pIdFieldName)); if("".equals(menuParentId)){ return parent; } //Parent node recursion newParent = (idFieldName,pIdFieldName,childrenFieldName,newParent,menuParentId,allList); return newParent; } public static <M> M toTreeByChildrenId(String cId, List<M> allList){ //Let's set the defaults. return ("id", "pid", "children",null,cId,allList); } /** * Splicing union sql script, based on query query criteria, id field name, pid field name, splicing sql *@param select Query field, e.g.: select id *@param tableName Table name, e.g. sys_dept *@param initWhere query conditions, e.g.: pid = '-1' *@param idField id field name *@param pidField pid field name *@param maxLevel Maximum level of splicing *maxLevel@return union spliced sql scripts */ public static String getParentSql(String select, String tableName, String initWhere, String idField, String pidField, int maxLevel) { return getUnionSql(select,tableName,initWhere,idField,pidField,maxLevel); } public static String getChildSql(String select, String tableName, String initWhere, String idField, String pidField, int maxLevel) { return getUnionSql(select,tableName,initWhere,pidField,idField,maxLevel); } private static String getUnionSql(String select, String tableName, String initWhere, String whereIn, String selectIn, int maxLevel) { StringBuilder stringBuilder = new StringBuilder(select); (" from ").append(tableName).append(" where 1=1 "); if (null != initWhere && !()) { (" and ").append(initWhere); } String tmp = ("from %s where %s", tableName, initWhere); for(int i = 0; i < maxLevel; ++i) { tmp = (" from %s where %s in ( select %s %s)", tableName, whereIn, selectIn, tmp); (" union ").append(select).append(tmp); } return (); } }