Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3983840
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:41:51+00:00 2026-05-20T05:41:51+00:00

e.g. I have a need where my ArrayList should contain hashmap at each index,

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:41:52+00:00Added an answer on May 20, 2026 at 5:41 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a basic class 'HistoryItem' like so: public class HistoryItem private Date startDate;
I need to make an ArrayList of ArrayLists thread safe. I also cannot have
I have an arraylist that contains items called Room. Each Room has a roomtype
I have an ArrayList which I need to sort and also I need a
I have a variable number of ArrayList's that I need to find the intersection
I have list of files stored inside arraylist that I need to download in
We have need for a rating system in a project we are working on,
I have need to select a number of 'master' rows from a table, also
I have the need to read the Thunderbird address book on the fly. It
I have a need to display a UITableView containing a user's account credentials. For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.