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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:57:27+00:00 2026-06-13T09:57:27+00:00

I have an Arraylist of Records. package com.demo.myproject; public class Records { String countryName;

  • 0

I have an Arraylist of Records.

package com.demo.myproject;

public class Records 
{
    String countryName;
    long numberOfDays;

    public String getCountryName() {
        return countryName;
    }
    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }
    public long getNumberOfDays() {
        return numberOfDays;
    }
    public void setNumberOfDays(long numberOfDays) {
        this.numberOfDays = numberOfDays;
    }

    Records(long days,String cName)
    {
        numberOfDays=days;
        countryName=cName;
    }
}

My Arraylist<Records> is containing the values

 Singapore     12 
 Canada         3
 United Sates  12
 Singapore     21

I need to modify it such that my output is

Canada         3
Singapore     33
United States 12

Please help me with solution,approach.

  • 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-06-13T09:57:27+00:00Added an answer on June 13, 2026 at 9:57 am

    You could store your Records in a Map, where the key would be the country.

    When you receive a new Record, check if the country already is in the map, if it is, add the number of days, if not create it.

    Map<String, Record> map = new HashMap<String, Record> ();
    
    addRecord(map, someRecord);
    
    private void addRecord(Map<String, Record> map, Record record) {
        Record inMap = map.get(record.getCountryName());
        if (inMap == null) {
            inMap = record;
        } else {
            inMap.setNumberOfDays(inMap.getNumberOfDays() + record.getNumberOfDays());
        }
        map.put(record.getCountryName(), inMap);
    }
    

    Notes:

    • I have assumed that it is fine to modify the records – if not just create a new one using the sum of the days.
    • you can still get the collection of records by calling map.values(); and iterate over them
    • ArrayList is not very well suited for your use case. If you really need to stick to ArrayList, for evey new record, you would need to loop over the list, check if one of the records in the list has the same country as the new record, update that record if you find it, or add a new record if not.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an arraylist which has a class datastructure. public class empRec { public
I have a object type list like List<Object> getRecords = records.getAllRecords(); ArrayList<String> recordsDetail =
I have arraylist myorderdata . I want to retrieve this arraylist to one String
At the moment inside the class I have ArrayList which the objects are stored
I have an ArrayList of HashMap like this: ArrayList<HashMap<String, String>> playListSongs = new ArrayList<HashMap<String,
I have an arraylist, that contains model class objects. Here is how I create
I have an ArrayList that import records from a database. Is there any method
I have to get the records from a table field where Length of record/data/string
I have arraylist of ids contains 1000 of records and this will be passed
I have an arraylist called fetchContactName() which returns all names (31) in my contact

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.