列表数据转化树

gradeChildren children parent
3 1 0
4 1 0
5 2 0
6 2 0


// 父亲层   Object 由 三个对象主键组成,孙子,孩子 父亲
Map<String, Object> parent = Maps.newHashMap();
List<Map<String, Object>> lstChildren = new ArrayList<Map<String, Object>>();
    Long id = Long.valueOf(getParameter("id"));
    List<Object> lstVipChildren = vipCustomerProfileService.findForVip(id);
    for (Object vipChildren : lstVipChildren) {
        Object[] obj = (Object[]) vipChildren;
        if (obj[0] != null) {
            List<Map<String, Object>> lstGradeChildren = new ArrayList<Map<String, Object>>();
            // 孙子层 (只有name属性 没有孩子)
            Map<String, Object> gradeChildren = Maps.newHashMap();
            gradeChildren.put("name", obj[0]);
            // 孩子层
            Map<String, Object> children = Maps.newHashMap();
            children.put("name", obj[1]);
            lstGradeChildren.add(gradeChildren);
            children.put("children", lstGradeChildren);
            boolean addNeed = true;
            for (int i = 0; i < lstChildren.size(); i++) {
                if (lstChildren.get(i).containsValue(obj[1])) {
                    addNeed = false;
                    List<Map<String, Object>> temp = (List<Map<String, Object>>) lstChildren.get(i).get("children");
                    temp.add(gradeChildren);
                    lstChildren.get(i).put("children", temp);
                }
            }
            if (addNeed) {
                lstChildren.add(children);
            }
        } else {
            if (obj[1] != null) {
                // 孩子层
                Map<String, Object> children = Maps.newHashMap();
                children.put("name", obj[1]);
                lstChildren.add(children);
            } else {
                break;
            }
        }
    }
    parent.put("name", getParameter("id"));
    parent.put("children", lstChildren);


你可能感兴趣的:(列表数据转化树)