将list中的元素按照属性分类成树状的map

 public LinkedHashMap<String, List<DevInfo>> queryList(List<DevInfo> list) {
       LinkedHashMap<String, List<DevInfo>> map = new LinkedHashMap<>();
       for (DevInfo li : list) {
//将需要归类的属性与map中的key进行比较,如果map中有该key则添加bean如果没有则新增key
           if (map.containsKey(li.getClassID())) {
//取出map中key对应的list并将遍历出的bean放入该key对应的list中
               ArrayList<DevInfo> templist = (ArrayList<DevInfo>) map.get(li.getClassID());
               templist.add(li);
           } else {
//创建新的list
               ArrayList<DevInfo> temlist = new ArrayList<DevInfo>();
               temlist.add(li);
               map.put(li.getClassID(), temlist);
           }
       }
       return map;
   }
 
 
 
  private List<Bean> initDatas(  List<?>  args)
    {
        List<Bean> mDatas = new ArrayList<Bean>();
        LinkedHashMap<String, List<DevInfo>> map = queryList(( List<DevInfo>)args);
        int parentID = 0;
        for (Object classID : map.keySet()) {
            List<DevInfo> list = map.get(classID);
            int id =0;
            String name = ClassType.getClassType( Integer.valueOf( classID.toString() )).getName();
            mDatas.add(new Bean(--parentID, -100 ,name+"("+list.size()+")" , null));
            for (DevInfo info:list   ) {
                mDatas.add(new Bean(id++, parentID ,name , info) );
            }
        }
        return mDatas;

    }

你可以使用递归来将一个具有父子关系的列表转换为树形结构。以下是一个示例代码,演示了如何将一个 `List<Map<String, Object>>` 转换为树形结构: ```java import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MapListToTree { public static void main(String[] args) { List<Map<String, Object>> mapList = new ArrayList<>(); Map<String, Object> map1 = new HashMap<>(); map1.put("id", 1); map1.put("name", "Node 1"); map1.put("parentId", null); mapList.add(map1); Map<String, Object> map2 = new HashMap<>(); map2.put("id", 2); map2.put("name", "Node 2"); map2.put("parentId", 1); mapList.add(map2); Map<String, Object> map3 = new HashMap<>(); map3.put("id", 3); map3.put("name", "Node 3"); map3.put("parentId", null); mapList.add(map3); List<Map<String, Object>> tree = buildTree(mapList, null); System.out.println(tree); } private static List<Map<String, Object>> buildTree(List<Map<String, Object>> nodes, Object parentId) { List<Map<String, Object>> tree = new ArrayList<>(); for (Map<String, Object> node : nodes) { Object nodeParentId = node.get("parentId"); if ((nodeParentId == null && parentId == null) || (nodeParentId != null && nodeParentId.equals(parentId))) { List<Map<String, Object>> children = buildTree(nodes, node.get("id")); node.put("children", children); tree.add(node); } } return tree; } } ``` 这段代码首先创建了一个 `mapList`,其包含了一些具有父子关系的节点。然后,通过调用 `buildTree` 方法,将 `mapList` 转换为树形结构。最后,输出转换后的树形结构。 请根据你的实际情况修改节点的属性和父子关系的表示方式。这只是一个示例,你可以根据需求进行适当的修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值