JAVA树状结构转List平面
使用递归实现
#主方法A 实体,@ApiModel(value = "A", description = "信息")@Datapublic class A{@ApiModelProperty(value = "级别,从1开始")private String level;@ApiModelProperty(value = "父节点编码")@Size(max = 30)@NotNullprivate String parentNode ;@ApiModelProperty(value = "子节点集合")@ValidList<A> children;}
##实现类//树转list List<A> needSaveList = new ArrayList<>(16);//递归createBodNodeList(needSaveList, null, X.getChildren(), 0);
/*** 树转list*/private void createBodNodeList(List<A> needSaveList, BodNodesAdd parentNode, List<A> children, int level, String bodNo) {if (children == null || children.size() == 0) {return;}level++;for (A inWarehouse : children) {A needSaveNode= BeanUtils.convert(inWarehouse, A.class);needSaveNode.setParentNodeNo(parentNode == null ? "" : parentNode.getNodeNo());needSaveNode.setLevel(level);needSaveList.add(needSaveBodNode);createBodNodeList(needSaveList, inWarehouse, inWarehouse.getChildren(), level);}}