Is there a best practice for writing maps literal style in Java?

前端 未结 9 1658
感情败类
感情败类 2020-12-24 03:03

In short, if you want to write a map of e.g. constants in Java, which in e.g. Python and Javascript you would write as a literal,

T CON         


        
9条回答
  •  囚心锁ツ
    2020-12-24 03:37

    I like to do it this way:

    Map map = new HashMap() {{
        put("foo", "bar");
        put(123, 456);
    }};
    

    The double {{ }} are an instance initialization block. They are a bit unusual but they are useful. No need for libraries or helpers.

提交回复
热议问题