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 6991759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:32:44+00:00 2026-05-27T19:32:44+00:00

I want to get all the objects from the list and put them into

  • 0

I want to get all the objects from the list and put them into a Map grouped by creation date, which means the map is like this: Map<String, List<MyObject>>. The MyObject object has a field that stores its creation date.

I’ve thought of doing a nested while loop that looks like this:

public Map<String, List<Expense>> getExpensesSorted(SortType type){
    Map<String, List<Expense>> map = new HashMap<String, List<Expense>>();

    List<Expense> expenses = getAllExpenses(budgetId).getExpenses()
            .getList();

    if (type.equals(SortType.DAY)) {
        Iterator<Expense> expIter = expenses.iterator();

        while (expIter.hasNext()) {
            List<Expense> list = new ArrayList<Expense>();
            Expense exp = (Expense) expIter.next();
            list.add(exp);

            String day = exp.getDate().format("YYYY-MM-DD");
            expIter.remove();

            while (expIter.hasNext()) {
                Expense exp2 = (Expense) expIter.next();
                if (exp2.getDate().format("YYYY-MM-DD").equals(day)) {
                    list.add(exp2);
                    expIter.remove();
                }
            }
            map.put(day, expenses);
        }

    } else if (type.equals(SortType.WEEK)) {
        ...
    } else if (type.equals(SortType.TYPE)) {
        ...
    } else if (type.equals(SortType.CATEGORY)) {
        ...
    }
    return map;
}

But this is wrong, it only gets all the ones that have the same day as the first element, so my map ends up having only one element.

I seriously don’t know how to solve this…

Thanks in advance for any help.

  • 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-27T19:32:44+00:00Added an answer on May 27, 2026 at 7:32 pm

    Something like this should do the job. I didn’t compile it though.

    while (iter.hasNext()) {
        MyObject obj = (MyObject) iter.next();
        String day = obj.getDate().format("YYYY-MM-DD");
    
        if(!map.containsKey(day)) {
           map.put(day, new ArrayList<MyObject>());
        }
        List<MyObject> list = map.get(day);
        list.add(obj);
        map.put(day, list);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get all the records from the one table which contain at
I have List of User object, I just want to get User objects from
I want get all of the Geom objects that are related to a certain
If I have Model.objects.all() I want to get only one object for any content_object=foo,
I want to get all conflict documents from a Notes database. So far, i've
I'm trying to find the most efficient way to get all objects from the
Is there any way to get a List<string> which contains all 'usings' within a
I need to get a list of all the objects within a module --
I want to get all values of a set interface in one go as
I want to get all the error messages out of the modelState without knowing

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.