在开发的很多场景中,需要我们对树形结构进行模糊查询,这个时候就需要用到递归来实现定位,效果如下图:
话不多说,直接上代码吧:
首先是对json的实例化对象处理
public List<PositionGroupModel> jsonToPositionGroupModel(JSONArray objArray){
List<PositionGroupModel> list = new ArrayList<>();
if (CollectionUtils.isEmpty(objArray)){
return list;
}
JSONArray children = JSONArray.parseArray(JSONObject.parseObject(objArray.get(0).toString()).get("children").toString());
children.forEach(chi -> {
//分类
JSONObject obj = (JSONObject)chi;
//公司级
JSONArray comObj = obj.getJSONArray("children");
List<PositionGroupModel> comList = new ArrayList<>();
comObj.forEach(com -> {
JSONObject c = (JSONObject)com;
//项目级
JSONArray positionGroupType = c.getJSONArray("positionGroupType");
List<PositionGroupModel> groupTypeList = new ArrayList<>();
positionGroupType.forEach( groupType -> {
JSONObject type = (JSONObject)groupType;
//岗位组
JSONArray positionGroup = type.getJSONArray("positionGroup");
List<PositionGroupModel> groupList = new ArrayList<>();
positionGroup.forEach(group -> {
JSONObject g = (JSONObject)group;
PositionGroupModel groupModel = new PositionGroupModel();
groupModel.setCode(g.get("groupId").toString());
groupModel.setName(g.get("groupName").toString());
groupModel.setId(g.get("groupId").toString());
groupModel.setIsChlidren(Integer.valueOf(g.get("isChildren").toString()));
groupList.add(groupModel);
});
PositionGroupModel typeModel = new PositionGroupModel();
typeModel.setCode(type.get("groupTypeId").toString());
typeModel.setName(type.get("groupTypeName").toString());
typeModel.setId(type.get("groupTypeId").toString());
typeModel.setIsChlidren(Integer.valueOf(1));
typeModel.setChildren(groupList);
groupTypeList.add(typeModel);
});
PositionGroupModel comModel = new PositionGroupModel();
comModel.setCode(c.get("code").toString());
comModel.setName(c.get("name").toString());
comModel.setId(c.get("id").toString());
comModel.setIsChlidren(Integer.valueOf(1));
comModel.setChildren(groupTypeList);
comList.add(comModel);
});
// 十一局
PositionGroupModel model = new PositionGroupModel();
model.setCode(obj.get("code").toString());
model.setName(obj.get("name").toString());
model.setId(obj.get("id").toString());
model.setIsChlidren(Integer.valueOf(1));
model.setChildren(comList);
list.add(model);
});
return list;
}
?其次是对名称模糊查询进行递归操作:
public void treeMatch(List<PositionGroupModel> oneCategoryList,String keyword){
Iterator<PositionGroupModel> oneIter = oneCategoryList.iterator();
while (oneIter.hasNext()){
PositionGroupModel oneCategory = oneIter.next();
// 如果包含则什么也不做(不移除),否则就看二级目录
if(!oneCategory.getName().contains(keyword)){
List<PositionGroupModel> twoCategoryList = oneCategory.getChildren();
Iterator<PositionGroupModel> twoIter = twoCategoryList.iterator();
while (twoIter.hasNext()){
PositionGroupModel twoCategory = twoIter.next();
// 如果包含则什么也不做(不移除),否则就看三级目录
if(!twoCategory.getName().contains(keyword)){
List<PositionGroupModel> threeCategoryList = twoCategory.getChildren();
Iterator<PositionGroupModel> threeIter = threeCategoryList.iterator();
while (threeIter.hasNext()){
PositionGroupModel threeCategory = threeIter.next();
// 如果包含则什么也不做(不移除),否则就看四级目录
if (!threeCategory.getName().contains(keyword)){
List<PositionGroupModel> fourCategoryList = threeCategory.getChildren();
Iterator<PositionGroupModel> fourIter = fourCategoryList.iterator();
while (fourIter.hasNext()){
PositionGroupModel fourCategory = fourIter.next();
if(!fourCategory.getName().contains(keyword)){
fourIter.remove();
}
}
// 四级目录看完了,如果四级目录全部被移除,则移除三级目录
if(CollectionUtils.isEmpty(threeCategory.getChildren())){
threeIter.remove();
}
}
}
// 三级目录看完了,如果三级目录全部被移除,则移除二级目录
if(CollectionUtils.isEmpty(twoCategory.getChildren())){
twoIter.remove();
}
}
}
// 二级目录看完了,如果二级目录全部被移除,则移除一级目录
if(CollectionUtils.isEmpty(oneCategory.getChildren())){
oneIter.remove();
}
}
}
}
最后,附上实体类的代码:
public class PositionGroupModel implements Serializable {
private static final Long serialVersionUID = 1L;
private String id;
@ApiModelProperty(value = "名称")
private String name;
@ApiModelProperty(value = "编码")
private String code;
/**
* 类型
*/
private Integer type;
private Integer isChlidren;
private List<PositionGroupModel> children;
希望能对大家的开发有所帮助,我是空谷有来人,谢谢支持。
|