JAVAList和Tree互转list泛型对象中要有pid和当前对象泛型的list Child
public class BgNatureGroup {
@ApiModelProperty(value = "唯⼀id", name = "id")
private String id;
@ApiModelProperty(value = "⽗id", name = "sPid")
private String sPid;
List<BgNatureGroup> children = new ArrayList<>();
}
list转Tree
List<BgNatureGroup> bgNatureGroups = bgNatureGroupDao.selectNatureGroup(param);
List<BgNatureGroup> natureGroupTree = buildValueTree(bgNatureGroups);
/
**
* 构造项⽬树形结构
*
* @param depts
* @return
*/
private List<BgNatureGroup> buildValueTree(List<BgNatureGroup> depts) {
Map<String, BgNatureGroup> entityMap = new HashMap<>();
List<BgNatureGroup> resultList = new ArrayList<>();
for (BgNatureGroup entity : depts) {
entity.setChildren(new ArrayList<>());
entityMap.Id(), entity);
}
for (BgNatureGroup entity : depts) {
BgNatureGroup parentEntity = (sPid());
if (StringUtils.isNull(parentEntity)) {
resultList.add(entity);
continue;
}
}
for (BgNatureGroup nv:resultList) {
Children().size()>0){
Children());
}
Children().size()==0){
nv.setChildren(null);
}
}
return resultList;
}
private void handleChildren(List<BgNatureGroup> d){
for (BgNatureGroup nv:d) {
Children().size()>0){
Children());
}
Children().size()==0){
nv.setChildren(null);
}
}
}
Tree转List
传参为Tree的集合
public ResponseBean saveGroupTree(List<BgNatureGroup> list) {
nodejs字符串转数组List<BgNatureGroup> natureGroups = treeToList(list, null);
return new ResponseBean(true, "操作成功", natureGroups);
}
/**
* 树形结构转list
*
* @param
* @return
*/
private List<BgNatureGroup> treeToList(List<BgNatureGroup> depts, String pid) {        List<BgNatureGroup> resultList = new ArrayList<>();
for (BgNatureGroup ip : depts) {
ip.setsPid(pid);
if (ip.getChildren() != null && ip.getChildren().size() > 0) {
resultList.addAll(Children(), ip.getId()));
ip.setChildren(null);
resultList.add(ip);
} else {
resultList.add(ip);
}
}
return resultList;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。