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


In short, if you want to write a map of the example constants in Java, for example Python and JavaScript Write as a verbatim,

  T & lt; String, string & gt; CONSTANTS = {"CONSTANT_NAME_0": CONSTANT_VALUE_0, "CONSTANT_NAME_1": CONSTANT_VALUE_1, "CONSTANT_NAME_2": CONSTANT_VALUE_2, // ...};  

Is there any class or any preset object that you can use to write data structures like that?

constants ? I use one.

  public enum constants {NAME_1 ("value1"), NAME_2 ("value2"), NAME_3 ("value 3"); Private string value; Stable (string value) {this.value = value; } Public string value () {return value; }}  

For example, the value NAME_2 can be obtained as follows:

  string name2value = Constants.NAME_2.value ();  

Just give enum a little more sensible name, e.g. Settings , Default , etc., whatever name / value pairs represent actually .


Comments