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

前端 未结 9 1859
感情败类
感情败类 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:51

    Here's another way, best suited for maps that won't be changing:

    public class Whatever {
        private static Map map = new HashMap();
    
        static {
            map.put("A", "Apple");
            map.put("B", "Banana");
            // etc
        }
    }
    

提交回复
热议问题