父级末级节点工具类
public class TreeUtil {
public static List < ProductCategory > getProductCategoryTreeList ( List < ProductCategory > entityList) {
List < ProductCategory > result = new ArrayList < > ( ) ;
for ( ProductCategory entity : entityList) {
Object parentId = entity. getParentId ( ) ;
if ( null == parentId || "" . equals ( parentId) || "0" . equals ( parentId) ) {
result. add ( entity) ;
}
}
for ( ProductCategory entity : result) {
String id = entity. getId ( ) ;
List < ProductCategory > children = getProductCategoryChildren ( id, entityList) ;
if ( null != children) {
entity. setChildren ( children) ;
} else {
entity. setChildren ( new ArrayList < ProductCategory > ( ) ) ;
}
}
return result;
}
public static List < ProductCategory > getProductCategoryChildren ( String id, List < ProductCategory > entityList) {
List < ProductCategory > childList = new ArrayList < > ( ) ;
String parentId;
for ( ProductCategory entity : entityList) {
parentId = entity. getParentId ( ) ;
if ( id. equals ( parentId) ) {
childList. add ( entity) ;
}
}
for ( ProductCategory entity : childList) {
String childId = entity. getId ( ) ;
if ( null != getProductCategoryChildren ( childId, entityList) ) {
entity. setChildren ( getProductCategoryChildren ( childId, entityList) ) ;
} else {
entity. setChildren ( new ArrayList < ProductCategory > ( ) ) ;
}
}
if ( childList. size ( ) == 0 ) {
return null ;
}
return childList;
}
public static List < Map < String , Object > > getTypeTreeList ( List < Map < String , Object > > entityList) {
List < Map < String , Object > > result = new ArrayList < > ( ) ;
for ( Map < String , Object > entity : entityList) {
Object parentId = entity. get ( "parentId" ) ;
if ( null == parentId || "" . equals ( parentId) || "0" . equals ( parentId) ) {
result. add ( entity) ;
}
}
for ( Map < String , Object > entity : result) {
String id = entity. get ( "key" ) + "" ;
List < Map < String , Object > > children = getTypeChildren ( id, entityList) ;
if ( null != children) {
entity. put ( "children" , children) ;
} else {
entity. put ( "children" , new ArrayList < Map < String , Object > > ( ) ) ;
}
}
return result;
}
public static List < Map < String , Object > > getTypeChildren ( String id, List < Map < String , Object > > entityList) {
List < Map < String , Object > > childList = new ArrayList < > ( ) ;
String parentId;
for ( Map < String , Object > entity : entityList) {
parentId = entity. get ( "parentId" ) + "" ;
if ( id. equals ( parentId) ) {
childList. add ( entity) ;
}
}
for ( Map < String , Object > entity : childList) {
String childId = entity. get ( "key" ) + "" ;
if ( null != getTypeChildren ( childId, entityList) ) {
entity. put ( "children" , getTypeChildren ( childId, entityList) ) ;
} else {
entity. put ( "children" , new ArrayList < Map < String , Object > > ( ) ) ;
}
}
if ( childList. size ( ) == 0 ) {
return null ;
}
return childList;
}
}