/**---------------------------------------构造tree-------------------------------------------*/
/**
* 方式二:使用递归实现
*
* @param treeList
* @return
*/
public static List<GlobalVo> toTree02(List<GlobalVo> treeList) {
List<GlobalVo> retList = new ArrayList<>();
for (GlobalVo parent : treeList) {
String pid = parent.getPid();
if (pid==null||"0".equals(pid)||"".equals(pid)) {
retList.add(findChildren(parent, treeList));
}
}
return retList;
}
private static GlobalVo findChildren(GlobalVo parent, List<GlobalVo> treeList) {
for (GlobalVo child : treeList) {
if (parent.getId().equals(child.getPid())) {
if (parent.getChildren() == null) {
parent.setChildren(new ArrayList<>());
}
parent.getChildren().add(findChildren(child, treeList));
}
}
return parent;
}
/**---------------------------------------子节点由下到上汇总,传入的是tree
* -------------------------------------------------------*/
public static GlobalVo calculateWithRecursion(List<GlobalVo> orgList) {
long l = System.nanoTime();
if (orgList == null || orgList.size() == 0) {
return null;
}
GlobalVo rootOrg = orgList.get(0);
calculateChildren(rootOrg);
return rootOrg;
}
private static GlobalVo calculateChildren(GlobalVo pNode) {
if (pNode == null || pNode.getChildren() == null
|| pNode.getChildren().size() == 0) {
return pNode;
}
double c = Double.valueOf(emptyNull(pNode.getUsedBudget()));
for (GlobalVo child : pNode.getChildren()) {
double count = Double.valueOf(emptyNull(calculateChildren(child).getUsedBudget()));
c += count;
}
pNode.setUsedBudget(c+"");
//DecimalFormat df = new DecimalFormat("#0.00");
//System.out.println();
//pNode.setPercentBudget(df.format((Double.valueOf(emptyNull(pNode.getUsedBudget()))/Double.valueOf(emptyNull(pNode.getInitBudget())))));
return pNode;
}
public static String emptyNull(Object str) {
if (str==null||"".equals(str)){
return "0";
}else {
return str.toString();
}
}
--------------------基础类----------------------
ublic class GlobalVo implements Serializable {
private static final long serialVersionUID = 1L;
private List<GlobalVo> children;
private GlobalVo parent;
private String id;
private String pid;
private String name;
private String cbsName;
private String cbsCode;
private String parentCbsCode;
private String initBudget;
private String usedBudget;
private String percentBudget;
private String remark;
private double count;
}
【无标题】
最新推荐文章于 2024-04-24 22:52:03 发布