首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Java中的行集创建树的最佳方法

用Java中的行集创建树的最佳方法
EN

Stack Overflow用户
提问于 2016-05-20 19:05:41
回答 1查看 144关注 0票数 1

假设我在Java中有一个列表,如下所示:

代码语言:javascript
复制
[
    [A, AA, 10],
    [A, AB, 11],
    [B, BA, 20],
    [A, AA, 12],
]

我希望处理每一行以创建映射映射,这样我就可以按以下方式处理每行中的最后一个值:

代码语言:javascript
复制
{   
    A: { 
       AA: [10, 12]
       AB: [11]
    },
    B: {
       BA: [20]
    }
}

这样我就可以打电话了,比如:

代码语言:javascript
复制
for (int i : map.get("A").get("AA")) { ... }

当然,我可以遍历列表并手动创建映射。但是,这是一段非常难看的代码,很难将其推广到3,4,5,...,n列。

有什么聪明的方法来处理这些清单吗?某种图书馆或者别的什么我没想过的东西?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-20 20:56:06

还有一段很难看的代码:-)

代码语言:javascript
复制
class CustomTree {
    private final Map store;
    private final int length;

    public CustomTree(List<List<String>> source, int length) {
        if (length < 2)
            throw new IllegalArgumentException("Length must be greater than 2");

        this.length = length;
        this.store = new HashMap();
        for (int i = 0; i < source.size(); i++) {
            List<String> line = source.get(i);
            if (line.size() != length)
                throw new IllegalArgumentException(String.format("Line %d has wrong length", i));
        }

        for (List<String> line : source) {
            if (line.size() != length)
                throw new IllegalArgumentException("Not all lines have right length");

            accumulate(store, line);
        }
    }

    public void accumulate(Map parent, List<String> keys) {
        String key = keys.get(0);
        Object value = parent.get(key);
        if (keys.size() == 2) {
            parent.put(key, value != null
                    ? addToList((List) value, keys.get(1))
                    : addToList(new ArrayList(), keys.get(1)));
        } else {
            Map child;
            if (value != null) {
                child = (Map) value;
            } else {
                child = new HashMap();
                parent.put(key, child);
            }
            accumulate(child, keys.subList(1, keys.size()));
        }
    }

    private List addToList(List list, String key) {
        Integer intValue = Integer.valueOf(key);
        if (!list.contains(intValue))
            list.add(intValue);
        return list;
    }

    public List<Integer> get(List<String> keys) {
        if (keys.size() != (length - 1))
            throw new IllegalArgumentException("Bad keys length");
        return get(keys, store);
    }

    private List<Integer> get(List<String> keys, Map tree) {
        Object object = tree.get(keys.get(0));
        if (object == null)
            return new ArrayList<Integer>(0);

        return keys.size() == 1
                ? ((List<Integer>) object)
                : get(keys.subList(1, keys.size()), (Map) object);
    }
}

用法

代码语言:javascript
复制
public class Main {
    public static void main(String[] args) {
        List<List<String>> source = new ArrayList<List<String>>();

        List<String> first = Arrays.asList("A", "AA", "CB", "10");
        List<String> second = Arrays.asList("A", "AB", "CB", "11");
        List<String> third = Arrays.asList("BA", "BA", "CB", "20");
        List<String> fourth = Arrays.asList("A", "AA", "CB", "12");
        List<String> fifth = Arrays.asList("BA", "BA", "CB", "21");

        source.add(first);
        source.add(second);
        source.add(third);
        source.add(fourth);
        source.add(fifth);

        CustomTree tree = new CustomTree(source, 4);
        System.out.println(tree.get(Arrays.asList("BA", "BA", "CB")));
        System.out.println(tree.get(Arrays.asList("BA", "B", "sf")));
    }
}

可能太丑了。只有在不需要任何中间映射的树的最终元素时,它才能工作。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37354151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档