转自:java树型数据结构的数据向上汇总_孺子牛牛的博客-CSDN博客_java 树节点 汇总
做了小优化
见demo
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Classname TaskProgressUpTest
* @Description
* @Date 2022/6/21 14:51
* @Author ww
*/
public class TaskProgressUpTest {
// g1:45
// g2:50
// g3:20
// g7:20
// g8:20
// g9:20
// g12:20
// g5:20
// g4:80
// g10:80
// g11:80
// g6:40
// g13:40
// g16:95
// g17:95
// g14:90
// g15:90
// g18:85
// g19:95
public static void main(String[] args) {
List<Goal> listGoal = new ArrayList<Goal>();
listGoal.add(new Goal(1, 0, "g1", null, null));//45
listGoal.add(new Goal(2, 1, "g2", null, null));//50
listGoal.add(new Goal(3, 2, "g3", null, null));//20
listGoal.add(new Goal(5, 3, "g5", 20, null));
listGoal.add(new Goal(7, 3, "g7", null, null));//20
listGoal.add(new Goal(8, 7, "g8", 20, null));
listGoal.add(new Goal(9, 7, "g9", 20, null));
listGoal.add(new Goal(12, 7, "g12", 20, null));
listGoal.add(new Goal(4, 2, "g4", null, null));//80
listGoal.add(new Goal(10, 4, "g10", null, null));//80
listGoal.add(new Goal(11, 10, "g11", 80, null));
listGoal.add(new Goal(6, 1, "g6", null, null));//40
listGoal.add(new Goal(13, 6, "g13", 40, null));
listGoal.add(new Goal(14, 0, "g14", null, null));
listGoal.add(new Goal(15, 14, "g15", null, null));
listGoal.add(new Goal(18, 15, "g18", 85, null));
listGoal.add(new Goal(19, 15, "g19", 95, null));
listGoal.add(new Goal(16, 0, "g16", null, null));
listGoal.add(new Goal(17, 16, "g17", 95, null));
List<Goal> fisrtFinishRoots = new ArrayList<>();
TaskProgressUpTest t = new TaskProgressUpTest();
Goal tempGoal = t.calculateValue(listGoal, fisrtFinishRoots);
t.printGoal(tempGoal);
for(Goal goal : fisrtFinishRoots){
t.printGoal(goal);
}
}
//找到最底层的叶子节点
public List<Goal> getBottomNode(List<Goal> listGoal) {
Map<Integer, Goal> map = new HashMap<Integer, Goal>();
for (Goal g : listGoal) {
map.put(g.getId(), g);
}
for (Goal g : listGoal) {
int pid = g.getParentId();
if (map.containsKey(pid)) {
map.remove(pid);
}
}
return new ArrayList<Goal>(map.values());
}
public List<Goal> setParentValue(List<Goal> listAllGoal, List<Goal> listBottomGoal, List<Goal> fisrtFinishRoots) {
Map<Integer, List<Goal>> map = new HashMap<Integer, List<Goal>>();
for (Goal g : listBottomGoal) {
int pid = g.getParentId();
List<Goal> listGoal = map.get(pid);
if (listGoal == null) {
listGoal = new ArrayList<Goal>();
}
listGoal.add(g);
map.put(pid, listGoal);
}
for (Integer i : map.keySet()) {
List<Goal> tempListGoal = map.get(i);
int result = 0;
for (Goal g : tempListGoal) {
result += g.getAverageScore();
}
for (Goal g : listAllGoal) {
int id = g.getId();
if (id == i) {
List<Goal> tempList = g.getListGoal();
if (tempList != null) {
tempListGoal.addAll(tempList);
}
g.setListGoal(tempListGoal);
int score = g.getAverageScore();
score = (score + result) / tempListGoal.size();
g.setAverageScore(score);
}
}
}
for (Goal g : listBottomGoal) {
if(g.getParentId().intValue() == 0){
fisrtFinishRoots.add(g);
}
listAllGoal.remove(g);
}
return listAllGoal;
}
public Goal calculateValue(List<Goal> listGoal, List<Goal> fisrtFinishRoots) {
if(listUnitEngineeringTask.size() == 0){
return null;
}
if (listGoal.size() == 1) {
return listGoal.get(0);
} else {
List<Goal> listBottomGoal = getBottomNode(listGoal);
List<Goal> list = setParentValue(listGoal, listBottomGoal, fisrtFinishRoots);
return calculateValue(list, fisrtFinishRoots);
}
}
public void printGoal(Goal goal) {
System.out.println(goal.getGoalName() + ":" + goal.getAverageScore());
List<Goal> listGoal = goal.getListGoal();
if (listGoal == null || listGoal.size() == 0) {
return;
}
for (Goal g : listGoal) {
printGoal(g);
}
}
}
计算进度以及状态工具类
package com.dchain.cloud.edd.busi.modules.fuxing.util;
import com.dchain.cloud.edd.busi.modules.fuxing.entity.UnitEngineeringTask;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Classname TaskProgressUpTest
* @Description
* @Date 2022/6/21 14:51
* @Author ww
*/
public class TaskProgressUpUtil {
/**
* 向上汇总进度of树结构,返回列表数据
* @param listDataOfTree
* @return 列表数据
*/
public static List<UnitEngineeringTask> upProgressOfTree(List<UnitEngineeringTask> listDataOfTree) {
List<UnitEngineeringTask> newListDataOfTree = new ArrayList<>();
//定义先完成的
List<UnitEngineeringTask> fisrtFinishRoots = new ArrayList<>();
//向上统计
TaskProgressUpUtil t = new TaskProgressUpUtil();
UnitEngineeringTask tempUnitEngineeringTask = t.calculateValue(listDataOfTree, fisrtFinishRoots);
fisrtFinishRoots.add(tempUnitEngineeringTask);
//转换顶层节点
for(UnitEngineeringTask task : fisrtFinishRoots){
t.printUnitEngineeringTask(task, newListDataOfTree);
}
return newListDataOfTree;
}
/**
* 找到最底层的叶子节点
* @param listUnitEngineeringTask
* @return
*/
public List<UnitEngineeringTask> getBottomNode(List<UnitEngineeringTask> listUnitEngineeringTask) {
Map<Long, UnitEngineeringTask> map = new HashMap<Long, UnitEngineeringTask>();
for (UnitEngineeringTask g : listUnitEngineeringTask) {
map.put(g.getId(), g);
}
for (UnitEngineeringTask g : listUnitEngineeringTask) {
long pid = g.getParentId();
if (map.containsKey(pid)) {
map.remove(pid);
}
}
return new ArrayList<UnitEngineeringTask>(map.values());
}
/**
* 计算父节点
* @param listAllUnitEngineeringTask
* @param listBottomUnitEngineeringTask
* @param fisrtFinishRoots
* @return
*/
public List<UnitEngineeringTask> setParentValue(List<UnitEngineeringTask> listAllUnitEngineeringTask, List<UnitEngineeringTask> listBottomUnitEngineeringTask, List<UnitEngineeringTask> fisrtFinishRoots) {
Map<Long, List<UnitEngineeringTask>> map = new HashMap<Long, List<UnitEngineeringTask>>();
for (UnitEngineeringTask g : listBottomUnitEngineeringTask) {
Long pid = g.getParentId();
List<UnitEngineeringTask> listUnitEngineeringTask = map.get(pid);
if (listUnitEngineeringTask == null) {
listUnitEngineeringTask = new ArrayList<UnitEngineeringTask>();
}
listUnitEngineeringTask.add(g);
map.put(pid, listUnitEngineeringTask);
}
for (Long i : map.keySet()) {
List<UnitEngineeringTask> tempListUnitEngineeringTask = map.get(i);
int result = 0;
for (UnitEngineeringTask g : tempListUnitEngineeringTask) {
result += g.getBuildRealProgress();
}
for (UnitEngineeringTask g : listAllUnitEngineeringTask) {
long id = g.getId();
if (id == i) {
List<UnitEngineeringTask> tempList = g.getChildList();
if (tempList != null) {
tempListUnitEngineeringTask.addAll(tempList);
}
g.setChildList(tempListUnitEngineeringTask);
double score = g.getBuildRealProgress();
score = (score + result) / tempListUnitEngineeringTask.size();
g.setBuildRealProgress(score);
}
}
}
for (UnitEngineeringTask g : listBottomUnitEngineeringTask) {
if(g.getParentId().intValue() == 0){
fisrtFinishRoots.add(g);
}
listAllUnitEngineeringTask.remove(g);
}
return listAllUnitEngineeringTask;
}
/**
* 从叶子节点开始,逐层递归计算
* @param listUnitEngineeringTask
* @param fisrtFinishRoots
* @return
*/
public UnitEngineeringTask calculateValue(List<UnitEngineeringTask> listUnitEngineeringTask, List<UnitEngineeringTask> fisrtFinishRoots) {
if(listUnitEngineeringTask.size() == 0){
return null;
}
if (listUnitEngineeringTask.size() == 1) {
return listUnitEngineeringTask.get(0);
} else {
List<UnitEngineeringTask> listBottomUnitEngineeringTask = getBottomNode(listUnitEngineeringTask);
List<UnitEngineeringTask> list = setParentValue(listUnitEngineeringTask, listBottomUnitEngineeringTask, fisrtFinishRoots);
return calculateValue(list, fisrtFinishRoots);
}
}
/**
* 打印每一条新数据并计算状态
* @param unitEngineeringTask
* @param newListDataOfTree
*/
public void printUnitEngineeringTask(UnitEngineeringTask unitEngineeringTask, List<UnitEngineeringTask> newListDataOfTree) {
// System.out.println(UnitEngineeringTask.getName() + ":" + UnitEngineeringTask.getBuildRealProgress());
//计算状态
unitEngineeringTask.setStatu(TaskStatuUtil.adapterStatu(unitEngineeringTask, unitEngineeringTask.getBuildRealProgress()));
newListDataOfTree.add(unitEngineeringTask);
List<UnitEngineeringTask> listUnitEngineeringTask = unitEngineeringTask.getChildList();
if (listUnitEngineeringTask == null || listUnitEngineeringTask.size() == 0) {
return;
}
for (UnitEngineeringTask g : listUnitEngineeringTask) {
printUnitEngineeringTask(g, newListDataOfTree);
}
}
}