e.g. I have a need where my ArrayList should contain hashmap at each index, e.g.
Public class Testing {
private ArrayList < < HashMap< String, String>> myData ;
public static void main(String[] args) {
myData = new ArrayList < HashMap < String, String>>();
initialize();
//After initialize myData should contain different Hashmaps.
}
public static void initialize() {
for (int i= 0; i < 10 ;i++) {
myMap = new HashMap(); //Bad because,creating Objects inside the loop.
myMap.put("title", "abc"+i); //Adding i, just to show that new values are stored everytime
myMap.put("name", "xyz"+i);
myData.add(myMap);
}
}
}
Above code will create hash map objects every time in the loop, what is the better way to do it, so that I can avoid creation of objects every time ? I am using this code in Android , but I think this is more general and related to Java more.