如何对Map Map使用双括号初始化 [英] How to use double brace initialization for Map of Map

查看:185
本文介绍了如何对Map Map使用双括号初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实知道双括号初始化有其自身的隐藏成本,但仍然有一种初始化Map<String,Map<String,String>>()的可能方法.

I do understand that double brace initialization has its own hidden cost, still is there a possible way to initialize Map<String,Map<String,String>>().

我尝试过的事情:

Map<String, Map<String, String>> defaultSourceCode = new HashMap<String, Map<String, String>>(){
            {"a",new HashMap<String, String>(){{"c","d"}}}
        };

我知道这是一个不好的做法,但至于实验,我正在尝试.

I know it is a bad practice but as for experiment i am trying it.

参考和动机: Arrays.asList也适用于地图吗?

推荐答案

几乎一切都很好,您只需在大括号中使用方法调用即可:

Almost everything is fine, you just have to use method calls in double braces:

Map<String, Map<String, String>> defaultSourceCode = new HashMap<String, Map<String, String>>(){
    {put("a",new HashMap<String, String>(){{put("c","d");}});}
};

但是此答案描述了为什么你不应该那样做.

But this answer describes, why you shouldn't do that.

这篇关于如何对Map Map使用双括号初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆