zoukankan      html  css  js  c++  java
  • 集合,实现递归

    最近有做到一个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);

  • 相关阅读:
    Java循环结构
    Java正则表达式
    Java日期时间
    Java如何输入数据
    Java数组
    Java StringBuffer和StringBuilder类
    Java String类
    Java Character类
    Java Number&Math类
    python之二维码生成
  • 原文地址:https://www.cnblogs.com/Snowflake/p/3412190.html
Copyright © 2011-2022 走看看