java 统计子节点值赋值给父节点

package org.zkpk.cs;

import java.util.ArrayList;
import java.util.List;

import net.bytebuddy.dynamic.scaffold.MethodRegistry.Handler.ForImplementation;

public class test {
	public static void main(String[] args) {
		List list = new ArrayList<>();
		Dto o1 = new Dto();
		o1.setId("1");
		o1.setPid("-1");
		o1.setNum(0);
		list.add(o1);
		
		Dto o2 = new Dto();
		o2.setId("1-1");
		o2.setPid("1");
		o2.setNum(0);
		list.add(o2);
		
		Dto o3 = new Dto();
		o3.setId("1-2");
		o3.setPid("1");
		o3.setNum(0);
		list.add(o3);
		
		Dto o4 = new Dto();
		o4.setId("1-1-1");
		o4.setPid("1-1");
		o4.setNum(4);
		list.add(o4);
		
		Dto o5 = new Dto();
		o5.setId("1-1-2");
		o5.setPid("1-1");
		o5.setNum(6);
		list.add(o5);
		
		Dto o6 = new Dto();
		o6.setId("1-1-2-1");
		o6.setPid("1-1-2");
		o6.setNum(1);
		list.add(o6);
		
		List addList = new ArrayList();
		addList.add(o1);
		
		doCount(o1, list, addList);
		
		for (Dto dto : addList) {
			System.out.println(dto.getPid() + "=========" + dto.getId() + "=============" + dto.getNum());
		}
	}

	public static void  doCount(Dto dto ,List orglist,List addlist){
		int num = dto.getNum();
	     List clist= findByQueryString(dto.getId(),orglist);
	     if(clist != null && clist.size()>0){
	        addlist.addAll(clist);
	        for (Dto child : clist) {
	              //递归
	              doCount(child,orglist,addlist);
	              num += child.getNum();
	              dto.setNum(num);
	           }
	        }
	  }
	
	 public static List findByQueryString(String pid ,List orglist){
		 List list = new ArrayList<>();
		 if(orglist != null && orglist.size() > 0 ) {
			 for (Dto dto : orglist) {
		            if(dto.getPid().equals(pid)){
		               list.add(dto);
		            }
		         }
		 }
         
         return list;
      }
}

运行结果:

-1=========1=============11
1=========1-1=============11
1=========1-2=============0
1-1=========1-1-1=============4
1-1=========1-1-2=============7
1-1-2=========1-1-2-1=============1

 

你可能感兴趣的:(java 统计子节点值赋值给父节点)