直接撸代码
```java
public static void main(String[] args) {
ArrayList<MetricRepoTree> parentList = Lists.newArrayList();
ArrayList<MetricRepoTree> result = Lists.newArrayList();
ArrayList<MetricRepoTree> child1List = Lists.newArrayList();
ArrayList<MetricRepoTree> subChild1List = Lists.newArrayList();
ArrayList<MetricRepoTree> child2List = Lists.newArrayList();
MetricRepoTree metricRepoTreeParent1 = new MetricRepoTree();
MetricRepoTree metricRepoTreeParent2 = new MetricRepoTree();
metricRepoTreeParent1.setId("111");
metricRepoTreeParent2.setId("222");
parentList.add(metricRepoTreeParent1);
parentList.add(metricRepoTreeParent2);
MetricRepoTree metricRepoTreeChild11 = new MetricRepoTree();
MetricRepoTree metricRepoTreeSubChild111 = new MetricRepoTree();
MetricRepoTree metricRepoTreeSubChild112 = new MetricRepoTree();
metricRepoTreeSubChild111.setId("111-111-111");
metricRepoTreeSubChild112.setId("111-111-222");
subChild1List.add(metricRepoTreeSubChild111);
subChild1List.add(metricRepoTreeSubChild112);
metricRepoTreeChild11.setChildren(subChild1List);
MetricRepoTree metricRepoTreeChild12 = new MetricRepoTree();
MetricRepoTree metricRepoTreeSubChild121 = new MetricRepoTree();
MetricRepoTree metricRepoTreeSubChild122 = new MetricRepoTree();
metricRepoTreeSubChild121.setId("111-222-111");
metricRepoTreeSubChild122.setId("111-222-222");
metricRepoTreeChild12.setChildren(Lists.newArrayList(metricRepoTreeSubChild121,metricRepoTreeSubChild122));
metricRepoTreeChild11.setId("111-111");
metricRepoTreeChild12.setId("111-222");
child1List.add(metricRepoTreeChild11);
child1List.add(metricRepoTreeChild12);
metricRepoTreeParent1.setChildren(child1List);
MetricRepoTree metricRepoTreeChild21 = new MetricRepoTree();
MetricRepoTree metricRepoTreeChild22 = new MetricRepoTree();
metricRepoTreeChild21.setId("222-111");
metricRepoTreeChild22.setId("222-222");
child2List.add(metricRepoTreeChild21);
child2List.add(metricRepoTreeChild22);
metricRepoTreeParent2.setChildren(child2List);
Map<String,String> resPermissionsMap = Maps.newHashMap();
resPermissionsMap.put("111-111-111","view");
resPermissionsMap.put("111-222","view");
if(!CollectionUtils.isEmpty(parentList)){
parentList.forEach(metricRepoTree -> {
String parentPermission = resPermissionsMap.get(metricRepoTree.getId());
metricRepoTree.setPermissions(parentPermission);
metricRepositoryTree(metricRepoTree,resPermissionsMap);
if(!CollectionUtils.isEmpty(metricRepoTree.getChildren()) || !StringUtils.isEmpty(parentPermission)){
result.add(metricRepoTree);
}
});
}
System.out.println(JsonUtil.encode(result));
}
private static boolean metricRepositoryTree(MetricRepoTree metricRepoTree, Map<String, String> resPermissionsMap) {
if (null != metricRepoTree) {
List<MetricRepoTree> childMetricRepoList = metricRepoTree.getChildren();
if (null != childMetricRepoList && !childMetricRepoList.isEmpty()) {
Iterator<MetricRepoTree> it = childMetricRepoList.iterator();
while(it.hasNext()){
MetricRepoTree childMetric = it.next();
String permission = resPermissionsMap.get(childMetric.getId());
childMetric.setPermissions(permission);
boolean isWithoutAuthorize = CollectionUtils.isEmpty(childMetric.getChildren())
&& StringUtils.isEmpty(permission);
if (isWithoutAuthorize || metricRepositoryTree(childMetric, resPermissionsMap)) {
it.remove();
}
}
if (CollectionUtils.isEmpty(childMetricRepoList) && StringUtils.isEmpty(metricRepoTree.getPermissions())) {
return true;
}
}
}
return false;
}