목록 데이터 변환 트리

2116 단어
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);

좋은 웹페이지 즐겨찾기