0

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.

6
  • 2
    Why do you want to avoid it? Modern JVMs are very good at this. Do you have evidence that it's a problem for your application? Commented Feb 16, 2011 at 8:45
  • 1
    Ok , I am using this code in android , mobile operating system , here Garbage collector kicks in every now and then , so have to avoid creation of objects for performance and optimal usage of memory. Commented Feb 16, 2011 at 8:49
  • 1
    I don't believe the GC will kick in as a result of this code (assuming it compiled) even on a phone. Commented Feb 16, 2011 at 8:58
  • ok , considering that many experienced programmers are indicating that , this is not a bad way. I will not do any changes for my requirement Thanks everyone. Commented Feb 16, 2011 at 9:37
  • @sat if you use android, you should tag the question that way Commented Feb 16, 2011 at 9:40

3 Answers 3

5

If you need a List of Maps, then that is what you will have to do, there is no way around that. (Actually there is, you could write a helper method that initializes the maps when first accessed and only access the map through that helper method, but I wouldn't really recommend that).

But you could rethink your design and use a different data structure, perhaps turn the Map / List relation around and use a Guava Multimap. That will only initialize the collections when they are needed, and you can save your initialization routine.

Also, a List of Maps can often be replaced by a List of custom objects. Depending on whether the keys are dynamic or not, a custom object may make more sense.

Sign up to request clarification or add additional context in comments.

Comments

1

Usually clarity is more important than performance. In this example, having it compile would be an improvement. ;)

You are not creating enough objects to really matter, but one way to reduce its consumption is to use a POJO instead of a HashMap.

3 Comments

As I'm learning from my recent (and traumatic) foray into Android programming, clarity often has to take a back seat to performance. It makes the Baby Jesus cry.
Agreed. However, the GC on Android is awful. It's like stepping back into the late 90s.
compiling above code is not necessary for me , as it is just a snippet of code from my original code. Thanks once again , Will search for optimization in other methods :)
0

I don't think it's bad.

Since you want an ArrayList<<HashMap<String, String>>, there's nothing wrong. Even no bad smell:)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.