风扬  

最近有做到一个tree  :返回给前端的时候需要一一个节点的形式返回,原做法是 取出所有的节点元素,进行递归遍历,层层存储:考虑到效率,特优化为以下方法:直接贴代码

velocity 实现递归的方式 是新建一个test.vm,然后在vm 里边#path(test.vm)

List<CateGroupTreeNode> cateNodes=util.getTreeNodes(categorys);

Map<Integer,CateGroupTreeNode> categoryMap=new HashMap<Integer, CateGroupTreeNode>();//类目对应的节点
for (CateGroupTreeNode node : cateNodes) {
  categoryMap.put(node.getId(), node);
}

for (CateGroupTreeNode node : groupNodes) {
  if(map.containsKey(node.getParentId())){
    map.get(node.getParentId()).getChild().add(node);
  }else{
    if(node.getParentId()==0){
      root.getChild().add(node);
    }
  }
}

 

 

整理下:集合的常用方法:

A,B交集     list1.retainAll(list2);    list1 数是获取的交集

A,B 并集

  list1.removeAll(list2);

     list1.addAll(list2);

     方法二: 集合数据存入 set

A,B差集

Set<Integer> allCatId=categoryMap.keySet();
Set<Integer> unGroupIds=new HashSet<Integer>(); //未分类的 类目ID集
unGroupIds.addAll(allCatId);
unGroupIds.removeAll(catIds);

 

 

posted on 2013-11-07 11:57  风扬扬  阅读(357)  评论(0编辑  收藏  举报