关于java:容器之Map构造优化

起因

在我工作过程中,始终会碰到这样一个问题,把程序运行过程中的数据,保护在内存中,不便下次查找。最近才发现自己的写法始终有问题。

之前的写法

    public static void main(String[] args) {
        List<String> names = Stream.of("aaa","bbb","ccc","bbb").collect(Collectors.toList());
        Map<String, Integer> result = new HashMap<>(8);
        for (String name : names) {
            if(result.containsKey(name)){
                result.put(name, result.get(name) + 1);
            }else{
                result.put(name, 1);
            }
        }
        System.out.println(result);
    }

下面这种写法,在对于简略数据处理上还能承受,然而对于简单数据,比方3层构造,写起来就比拟凌乱了。

    public static void main(String[] args) {
        List<String> lunch1 = Stream.of("cjq","2010710","咖喱").collect(Collectors.toList());
        List<String> lunch2 = Stream.of("cjq","2010711","牛肉").collect(Collectors.toList());
        List<String> lunch3 = Stream.of("cbx","2010710","咖喱").collect(Collectors.toList());
        List<String> lunch4 = Stream.of("cbx","2010710","鸡肉").collect(Collectors.toList());
        List<List<String>> all = new LinkedList<>();
        all.add(lunch1);
        all.add(lunch2);
        all.add(lunch3);
        all.add(lunch4);
        Map<String, Map<String, String>> result = new HashMap<>(8);
        for (List<String> lunch : all) {
            if(result.containsKey(lunch.get(0))){
                Map<String, String> day = result.get(lunch.get(0));
                if(day.containsKey(lunch.get(1))){
                    day.put(lunch.get(1), day.get(lunch.get(1)) + "," + lunch.get(2));
                }else {
                    day.put(lunch.get(1), lunch.get(2));
                }
            }else{
                Map<String, String> day = new HashMap<>(8);
                day.put(lunch.get(1), lunch.get(2));
                result.put(lunch.get(0), day);
            }
        }
        System.out.println(result);
    }

代码改良

    public static void main(String[] args) {
        List<String> names = Stream.of("aaa","bbb","ccc","bbb").collect(Collectors.toList());
        Map<String, Integer> result = new HashMap<>(8);
        for (String name : names) {
            result.put(name, result.getOrDefault(name, 0) + 1);
        }
        System.out.println(result);
    }
    public static void main(String[] args) {
        List<String> lunch1 = Stream.of("cjq", "2010710", "咖喱").collect(Collectors.toList());
        List<String> lunch2 = Stream.of("cjq", "2010711", "牛肉").collect(Collectors.toList());
        List<String> lunch3 = Stream.of("cbx", "2010710", "咖喱").collect(Collectors.toList());
        List<String> lunch4 = Stream.of("cbx", "2010710", "鸡肉").collect(Collectors.toList());
        List<List<String>> all = new LinkedList<>();
        all.add(lunch1);
        all.add(lunch2);
        all.add(lunch3);
        all.add(lunch4);
        Map<String, Map<String, String>> result = new HashMap<>(8);
        for (List<String> lunch : all) {
            Map<String, String> day = result.getOrDefault(lunch.get(0), new HashMap<>());
            String food = day.getOrDefault(lunch.get(1), null);
            if (food != null) {
                day.put(lunch.get(1), food + "," + lunch.get(2));
            }else{
                day.put(lunch.get(1), lunch.get(2));
            }
            result.put(lunch.get(0), day);
        }
        System.out.println(result);
    }

应用到了map提供的接口getOrDefault。

源码解析


588行:判断了这个key是否为空,如果没有就返回咱们传入的默认值,简化了代码逻辑。

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理