I now need to put a hashmap in a class, put some data inside, each time to fetch data from the database to find the HashMap, see if there is already exist, if there is a direct extraction, if not exist to extract data from the database and then put into HashMap, Should you set the HashMap as a static variable for the class?
If you set public static HashMap Hmdata, you will not be able to add data to the Hmspace, should I new an object?
Common declarative methods (using static blocks of code):
Java code
- Public final static Map map = new HashMap ();
- static {
- Map.put ("Key1", "value1");
- Map.put ("Key2", "value2");
- }
A simple way to:
Java code
- Public final static Map map = new HashMap () {{
- Put ("Key1", "value1");
- Put ("Key2", "value2");
- }};
This approach is equivalent to an anonymous implementation of the overloaded HashMap. An anonymous construction method was added to the original HashMap.
Another way to add a List/set variable is to declare it:
Java code
- Public final static List List = Arrays.aslist ("Elment1", "Element2");
A simple way to declare static map constants in Java