1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| package tt;
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
public class XxfMain {
public static List<Xmzb> allList = new ArrayList<Xmzb>();
public static void main(String[] args) { Xmzb root = new Xmzb("1", "0", 0); Xmzb root1_1 = new Xmzb("1-1", "1", 3999); Xmzb root1_2 = new Xmzb("1-2", "1", 1); Xmzb root1_1_1 = new Xmzb("1-1-1", "1-1", 1); Xmzb root1_1_2 = new Xmzb("1-1-2", "1-1", 0); Xmzb root1_2_1 = new Xmzb("1-2-1", "1-2", 1); Xmzb root1_2_2 = new Xmzb("1-2-2", "1-2", 0); allList.add(root); allList.add(root1_1); allList.add(root1_2); allList.add(root1_1_1); allList.add(root1_1_2); allList.add(root1_2_1); allList.add(root1_2_2); List<Xmzb> list = new ArrayList<Xmzb>(); list.add(root); doCount(root, list); for (Xmzb xmzb : allList) { System.out.println(xmzb.getId() + ":" + xmzb.getXmgs()); }
}
public static Map<String, Object> doCount(Xmzb root, List<Xmzb> list) { Map<String, Object> map = new HashMap<String, Object>(); Double xmgs = 0.0;
List<Xmzb> clist = findByQueryString(root.getId());
if (clist == null || clist.size() == 0) { if (null != root.getXmgs()) { map.put("xmgs", root.getXmgs()); } else { map.put("xmgs", xmgs); } return map; }
if (clist != null && clist.size() > 0) { list.addAll(clist); for (Xmzb child : clist) { Map<String, Object> map_c = doCount(child, list); Double res = Double.parseDouble(map_c.get("xmgs").toString()); child.setXmgs(res.intValue()); xmgs += res; }
} root.setXmgs(root.getXmgs() + xmgs.intValue()); map.put("xmgs", root.getXmgs()); map.put("list", list); return map; }
public static List<Xmzb> findByQueryString(String pid) { List<Xmzb> list = new ArrayList<Xmzb>(); for (Xmzb xmzb : allList) { if (xmzb.getPid().equals(pid)) { list.add(xmzb); } } return list;
}
}
|