foreachjdk8递归_Java8stream流操作重构递归代码原代码:
private static JSONArray produceTree(List resources,boolean needPermission){
JSONArray tree = new JSONArray();
if(resources!=null&&resources.size()>Number.ZERO) {//递归退出条件判断
for (Resource resource : resources) {
List resourceList=resourceService.Id());//根据⽗id查询⼦资源列表
JSONArray children = produceTree(resourceList,needPermission,staffName,roleId);//递归,⼀直向下查
JSONObject node=TreeUtil.spellTree(resource);//转换为具体的树形式
if(children!=null&&children.size()>Number.ZERO){
node.put("children",children);
}else if(needPermission){//需要该资源下对应的权限信息(最后⼀级,判断下⾯有没有权限关系)
node=Id(),node,staffName);
}
tree.add(node);
}
}
return tree;
}
Resource结构如下:
@Data
public class Resource {
private Integer id;//资源id
private String resourceName;//资源名称
java valueofprivate String url;//资源地址 可能⽆⽤
private String icon;//资源图标
private String clue;// 树形结构横向线索
private Integer distance; // 到根节点的距离 纵向线索
private Integer parentId;// ⽗资源id
private String description; //资源描述
private Date createtime;//创建时间
private Integer state;// 0未删除 1已删除
List children;
}
现在想⽤java8 Stream流重写这段代码,⼤概是这样:
private static List recursive(List resources){
return resources.stream()
.map(Resource::getId)//第⼀步转换为id
.map(parentId->resourceService.queryChildTreeByPid(parentId))//第⼆步查id下对应的资源(id相当于parentId使⽤)
.map(child->Optional.ofNullable(child).filter(obj -> true).ifPresent(isExists->recursive(isExists)))//第三步 如果childrenList不为null,递归此⽅法
.toArray(childrenL->reReousce.setChildren());//第四步 将得到的List 收集到上⼀个resource的children中 ,
resource.setChildren(childrenList)
}
第三步以后 编译出错
设想⼀下 第三步应该⽤toArray⽅法收集⼦资源 然后第四步将收集到的⼦资源放进对应的⽗资源resource中 请问如何实现呢
--------------感谢imanguo⼤佬的指点 还有某me姓⼤佬的指点 分享下我的树形结构⽣产源码-------
--------------Any library suggestions/code samples/guidance would be --------------
private static ResourceService resourceService;
private static PermissionService permissionService;
@Autowired
public TreeUtil(ResourceService resourceService,PermissionService permissionService){
this.permissionService=permissionService;
}
/**
* @Author: zms
* @Description: ⽣成菜单树结构
*
* clue是线索 例如 1-2-3 代表 根节点是1 1下⾯有2节点 2下⾯有3节点
* 根据收集根节点id 作为线索 可以去除持久化数据⾥⾯重复的资源
* 例如 同时有了 1节点 2节点 因为1是2的⽗节点 那么有了1就不需要2的存在了 有⽗节点就拥有其所有⼦节点
*
* @Date: Create on 2018/11/12 14:59
*/
public static List produceTree(List resources,boolean needPermission){
Map> listMap=partitionResource(resources);
List (true);//根节点资源树
List (false);//⾮根节点
String clue=nodeTree.stream().map(Resource::getId).map(String::valueOf).collect(joining("-","","-"));//根节点线索
List childTree=duplicateChildTree.stream().filter(resource -> !Clue())).collect(toList());//去除重复资源
nodeTree.addAll(childTree);//合并所有
List ursiveDown(nodeTree,needPermission);//向下递归查⼦节点
List ursiveUp(downTree);//向上递归查⽗节点
return merge(upTree);
}
/**
* @Author: zms
* @Description: 新递归⽣成树⽅法 上⾄下
*
* 5 ---
* 2 --- 6 ---
* 1--- 3 --- 7 ---
* 4 --- 8 ---
* 9 ---
*
* 以1开始查到2,3,4 然后2,3,4再次递归 2(5,6) 3(7) 4(8,9) 括号内是对应的children
* 为什么引⼊ResourceGroup? 将resource和ChildrenResource 向上抽象⼀层
* Resource中含有其他属性 ResourceGroup仅仅表⽰⽗⼦节点的关系 可以不使⽤ 看个⼈习惯
* @Date: Create on 2018/11/8 11:30
*/
public static List recursiveDown(List resources, boolean needPermission){
return resources
.parallelStream()
.map(resource -> ResourceGroup(resource,needPermission))
.peek(ResourceGroup::autoSet)
.map(ResourceGroup::getParent)
.List());
}
/**
* @Author: zms
* @Description: 转换成resouceGroup收集
* @Date: Create on 2018/11/8 11:30
*/
private static ResourceGroup toResourceGroup(Resource resource, boolean needPermission) { ResourceGroup resourceGroup=new ResourceGroup();
resourceGroup.setParent(resource);
List children = Id(),needPermission);
if(Objects.equals(0,children.size())&&needPermission){
List Id()); resourceGroup.setPermissions(permissions);
return resourceGroup;
}
resourceGroup.setChildren(children);
return resourceGroup;
}
/**
* @Author: zms
* @Description: 获取⽗节点对应的⼦资源
* @Date: Create on 2018/11/8 11:30
*/
private static List findChildren(Integer parentId,boolean needPermission){
List chidren=resourceService.queryChildTreeByPid(parentId);
recursiveDown(chidren,needPermission);
return chidren;
}
/**
* @Author: zms
* @Description: 新递归⽣成树⽅法 下⾄上
* @Date: Create on 2018/11/8 18:32
*/
private static List recursiveUp(List resources){
return resources.stream()
.map(TreeUtil::toResource)
.map(ResourceGroup::getParent)
.List());
}
/**
*@Author: zms
*@Description: 递归查⽗节点
*@Date: Create On 2018/11/10 14:33
*/
private static ResourceGroup toResource(Resource resource){
ResourceGroup resourceGroup=new ResourceGroup();
Resource parent=findParent(resource);
resourceGroup.setParent(parent);
return resourceGroup;
}
/**
* @Author: zms
* @Description: 获取收集好的⼦资源的⽗节点
* @Date: Create on 2018/11/8 18:37
*/
private static Resource findParent(Resource resource){
if(Objects.equals(ParentId())){
return resource;
}
Resource ParentId());
List children=new ArrayList<>();
children.add(resource);
parent.setChildren(children);
return recursiveUp(Collections.singletonList(parent)).stream().filter(Objects::nonNull).findFirst().orElse(resource); }
/**
* @Author: zms
* @Description:
*
* 业务背景: 假设id为1的资源节点下有 2,3,4 三个⼦资源 可能某个⾓⾊只有 2,3的资源树
* 2 -->1
* dulipcate
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论