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

  • SEARCH
  • Home
  • 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 1114485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:00:47+00:00 2026-05-17T03:00:47+00:00

I have page gets given an ArrayList<Document> where each document has a property called

  • 0

I have page gets given an ArrayList<Document> where each document has a property called type.

I don’t know the number of unique types or documents.

I want to sort this ArrayList into a HashMap<type, document[]> but am having some trouble getting my head around it.

Some pseudo-code would like like

for (int i = 0; i < documents.size(); i++) 
{
   if there is an array for documents[i].type
   add to this array
   else create a new array for this type
   add document[i].type and the array of documents with matching type to the hashmap
}

I know this is the wrong approach and clearly won’t work. I am open to any suggestions.

Thank you

  • 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-17T03:00:48+00:00Added an answer on May 17, 2026 at 3:00 am
    // create the map to store stuff, note I'm using a List instead of an array
    // in my opinion it's a bit cleaner
    Map<String, List<Document>> map = new HashMap<String, List<Document>>();
    
    // now iterate through each document
    for(Document d : documents){
    
        // check to see if this type is already known
        List<Document> list = map.get(d.type);
    
        if(list == null){
            // list is null when it wasn't found in the map
            // this is a new type, create a new list
            list = new ArrayList<Document>();
    
            // store the list in the map
            map.put(d.type, list);
        }
    
        // finally, whether we got a hit or a miss, we want
        // to add this document to the list for this type
        list.add(d);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dynamic page that gets a certain number of results for rooms
I have a html page with a form. The form has Div which gets
I have a function called on every single page: /// <summary> /// Gets the
I have a page where the user gets directed to if they want to
I have a datalist inside a usercontrol that gets loaded into a page where
I have this function in jQuery that gets data from a page with POST,
I have the following element on my page, which gets a dynamically added ID
On my page in IE8, I have got some text which gets changed through
I have a main page which gets the information from a local xml file
Ok I have a dynamically created set of elements where each particular kind gets

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.