/**
* @Description: 根据结构名称过滤树形结构
* @param epcEiInspectionStrucs 树形结构
* @param keyword 结构名称
* @return: void
*/
private static EpcEiInspectionStruc filterTree(EpcEiInspectionStruc epcEiInspectionStrucs ,String keyword){
if(isRemoveNode(epcEiInspectionStrucs,keyword)){
return null;
}
Iterator<EpcEiInspectionStruc> iterator = epcEiInspectionStrucs.getChildren().iterator();
while (iterator.hasNext()){
EpcEiInspectionStruc child = iterator.next();
deleteNode(child,iterator,keyword);
}
return epcEiInspectionStrucs;
}
private static boolean isRemoveNode(EpcEiInspectionStruc epcEiInspectionStruc,String keyword){
List<EpcEiInspectionStruc> children = epcEiInspectionStruc.getChildren();
if(CollectionUtils.isEmpty(children)){
return !epcEiInspectionStruc.getStrucName().contains(keyword);
}
if(epcEiInspectionStruc.getStrucName().contains(keyword)){
return false;
}
boolean flag=true;
for(EpcEiInspectionStruc child:children){
if(!isRemoveNode(child,keyword)){
flag=false;
break;
}
}
return flag;
}
private static void deleteNode(EpcEiInspectionStruc child, Iterator<EpcEiInspectionStruc> iterator,String keyword){
if(isRemoveNode(child,keyword)){
iterator.remove();
return;
}
List<EpcEiInspectionStruc> children = child.getChildren();
if(CollectionUtils.isEmpty(children)){
return;
}
Iterator<EpcEiInspectionStruc> iterator1 = children.iterator();
while (iterator1.hasNext()){
EpcEiInspectionStruc next = iterator1.next();
deleteNode(next,iterator1,keyword);
}
}
java 树形过滤
最新推荐文章于 2024-08-23 15:54:50 发布