tree树型结构统计每个层级的总数
2017-02-17 14:17
1451 查看
1、如下图所示红色框框里面的数字如何统计出来
条件:
已知条件为最下一个层级的总数
答:
1、获取所有分类信息列表
2、根据分类信息列表获取最下一级商品总数
3、反向累加子级数据到父级
int counts = 0;
List<TreeVO> treeVos = new ArrayList<>();
TreeVO treeVo = new TreeVO(0, "全部", "#", "fa fa-folder-o", "#", null, false, true, true, counts, d);
treeVos.add(treeVo);
for (ProductTypePo p : productTypes) {
TreeVO tree = null;
Map<String, Object> attr = new HashMap<>();
attr.put("level", p.getClassLvl());
attr.put("edit", true);
int count = 0;
if (request.isCountProduct()) {
// 组装模糊匹配key
count = null == countMap.get(String.valueOf(p.getClassId())) ? 0 : countMap.get(String
.valueOf(p.getClassId()));
counts += count;
attr.put("count", count);
}
if (p.getClassLvl().intValue() == 4) {
attr.put("child", false);
tree = new TreeVO(p.getClassId(), p.getClassNm(), p.getfClassId(), "fa fa-folder-o",
p.getClassLvl(), p.getClassType(), false, false, false, count, attr);
} else {
attr.put("child", true);
tree = new TreeVO(p.getClassId(), p.getClassNm(), p.getfClassId(), "fa fa-folder-o",
p.getClassLvl(), p.getClassType(), false, false, false, count, attr);
}
treeVos.add(tree);
}
d.put("count", counts);
Integer levl = 4;//最高层级
boolean flag = true;
while(flag){
levl-=1;
List<TreeVO> treeVoLst = new ArrayList<>();
for (TreeVO tree : treeVos) {
if(tree.getData().get("level").equals(levl)){
treeVoLst.add(tree);
}
}
totTreeCount(treeVos,treeVoLst);
if(levl==0)
flag = false;
}
private boolean totTreeCount(List<TreeVO> treeVos, List<TreeVO> treeVoLst) {
for (TreeVO treeVO1 : treeVoLst) {
int count = 0;
for (TreeVO treeVO2 : treeVos) {
if (treeVO1.getId().equals(treeVO2.getParent())) {
count += treeVO2.getCount();
}
}
treeVO1.setCount(count);
treeVO1.getData().put("count", count);
}
return true;
}
条件:
已知条件为最下一个层级的总数
答:
1、获取所有分类信息列表
2、根据分类信息列表获取最下一级商品总数
3、反向累加子级数据到父级
int counts = 0;
List<TreeVO> treeVos = new ArrayList<>();
TreeVO treeVo = new TreeVO(0, "全部", "#", "fa fa-folder-o", "#", null, false, true, true, counts, d);
treeVos.add(treeVo);
for (ProductTypePo p : productTypes) {
TreeVO tree = null;
Map<String, Object> attr = new HashMap<>();
attr.put("level", p.getClassLvl());
attr.put("edit", true);
int count = 0;
if (request.isCountProduct()) {
// 组装模糊匹配key
count = null == countMap.get(String.valueOf(p.getClassId())) ? 0 : countMap.get(String
.valueOf(p.getClassId()));
counts += count;
attr.put("count", count);
}
if (p.getClassLvl().intValue() == 4) {
attr.put("child", false);
tree = new TreeVO(p.getClassId(), p.getClassNm(), p.getfClassId(), "fa fa-folder-o",
p.getClassLvl(), p.getClassType(), false, false, false, count, attr);
} else {
attr.put("child", true);
tree = new TreeVO(p.getClassId(), p.getClassNm(), p.getfClassId(), "fa fa-folder-o",
p.getClassLvl(), p.getClassType(), false, false, false, count, attr);
}
treeVos.add(tree);
}
d.put("count", counts);
Integer levl = 4;//最高层级
boolean flag = true;
while(flag){
levl-=1;
List<TreeVO> treeVoLst = new ArrayList<>();
for (TreeVO tree : treeVos) {
if(tree.getData().get("level").equals(levl)){
treeVoLst.add(tree);
}
}
totTreeCount(treeVos,treeVoLst);
if(levl==0)
flag = false;
}
private boolean totTreeCount(List<TreeVO> treeVos, List<TreeVO> treeVoLst) {
for (TreeVO treeVO1 : treeVoLst) {
int count = 0;
for (TreeVO treeVO2 : treeVos) {
if (treeVO1.getId().equals(treeVO2.getParent())) {
count += treeVO2.getCount();
}
}
treeVO1.setCount(count);
treeVO1.getData().put("count", count);
}
return true;
}
相关文章推荐
- 根据下图,统计硬币的总数并估计每个硬币的面积。
- 项目开发-树形层级结构中的数量统计
- 如何修改栈结构统计每个DLL的函数使用信息
- 士兵排队---统计每个士兵看到后排士兵的总数
- sql for xml: 生成树型结构的xml文件 (sql for xml to tree )
- INSERT INTO mysql树统计查询层级,与显示树结构整理
- SPOJ - COT:Count on a tree (树型结构套主席树)
- 实现统计每个栏目下的文章总数的调用
- Java 读取一段英文文档统计每个单词出现的次数和单词的总数
- 项目开发-树形层级结构中的数量统计
- 路径字符串数据转化为树型层级对象,path to json tree
- Java 读取一段英文文档统计每个单词出现的次数和单词的总数
- Hibernate视频学习笔记(12)树型结构作业及HQL
- 关于Java中集合框架的层级结构理解
- 前台页面 bonsai tree view 插件--树形结构选择器使用实例总结
- C++统计一个文件中每个数字字符(0~9)出现的次数
- ListView 实现树形层级结构
- 统计字符串中每个字符的个数
- 基于AJAX的动态树型结构的设计与实现
- ※数据结构※→☆非线性结构(tree)☆============树 顺序存储结构(tree sequence)(十五)