从列表<对象[]>构建树关系映射的有效方法

当我查询我的数据库时,它会返回我,其中包含父级,子级和孙子级。我想将此树关系的映射构建为 。我想知道建立这种关系的最快方法。List<Object[]>object[]Map<Parent, Map<Child, List<GrandChild>>>


我可以通过将我的数据库查询分解为多个查询来做到这一点,例如首先构建,然后使用每个查询来检索孙子列表,但是如果元素计数很高,此选项将成倍增加我需要查询DB的次数,或者使用以下代码将List转换为Map。Map<Parent, List<Child>>List<Child>


private Map<Long, Map<String, List<String>>> objectListToMap(List<Object[]> list) {

        Map<Long, Map<String, List<String>>> hmap = new HashMap<>();

        for (Object[] object : list) {

            Map<String, List<String>> innerMap = new HashMap<>();

            if (!hmap.containsKey(Long.parseLong(object[0].toString()))) {

                hmap.put(Long.parseLong(object[0].toString()), innerMap);

            }

            for (Object[] object1 : list) {

                if (Long.valueOf(object[0].toString()).equals(Long.valueOf(object1[0].toString()))) {

                    if (!innerMap.containsKey(object1[1].toString())) {

                        innerMap.put(object1[1].toString(), new ArrayList<String>());

                    }

                    innerMap.get(object1[1].toString()).add(object1[2].toString());

                }

            }

            hmap.get(Long.valueOf(object[0].toString())).putAll(innerMap);

        }

        return hmap;

    }

请让我知道我如何减少处理时间。


婷婷同学_
浏览 62回答 1
1回答

慕的地6264312

在这种情况下,有两个相互运行的 for 循环,即 n*n 循环。我们可以在另一个 for 循环内删除一个 for 循环,并在外部运行第二个 for 循环,其中包含仅包含外部映射的每个键的对象。List<object>[]private Map<Long, Map<String, List<String>>> objectListToMap(List<Object[]> list) {&nbsp; &nbsp; &nbsp; &nbsp; List<Object[]> objList = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; Map<Long, Map<String, List<String>>> hmap = new LinkedHashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; Map<Long, List<Object[]>> tempmap = new LinkedHashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; for (Object[] object : list) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!hmap.containsKey(Long.parseLong(object[0].toString()))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hmap.put(Long.parseLong(object[0].toString()), new HashMap<String, List<String>>());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempmap.put(Long.parseLong(object[0].toString()), objList);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempmap.get(Long.parseLong(object[0].toString())).add(object);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for (Map.Entry<Long, List<Object[]>> entry : tempmap.entrySet()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<String, List<String>> innerMap = new LinkedHashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Object[] obj : entry.getValue()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!innerMap.containsKey(obj[1].toString())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; innerMap.put(obj[1].toString(), new ArrayList<String>());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; innerMap.get(obj[1].toString()).add(obj[2].toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hmap.get(entry.getKey()).putAll(innerMap);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return hmap;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java