user1737894
user1737894

Reputation:

Adding values of repeated elements in ArrayList

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.

Upvotes: 1

Views: 2045

Answers (4)

Shilpa
Shilpa

Reputation: 11

public class RecordsMain {

    static ArrayList<Records> al = new ArrayList<Records>();
    static boolean flag = false;

    public static void main(String[] args) {
        Records rec1 = new Records(12,"Singapore");
        Records rec2 = new Records(3,"Canada");
        Records rec3 = new Records(12,"United States");
        Records rec4 = new Records(21,"Singapore");

        addToList(rec1);
        addToList(rec2);
        addToList(rec3);
        addToList(rec4);

        for (int i = 0; i < al.size(); i++) {     
            System.out.println(al.get(i).getCountryName() + " :: " + al.get(i).getNumberOfDays());
        }
    }

    public static void addToList(Records records) {
        for (int i = 0; i < al.size(); i++) {     
            if(al.get(i).getCountryName().equals(records.getCountryName())) {
                al.get(i).setNumberOfDays(al.get(i).getNumberOfDays()+records.getNumberOfDays());
                flag=true;
            }
        }
        if (flag == false)
            al.add(records);
    }
}

Note:

  • The function addToList adds records and while adding itself checks whether the CountryNames are duplicate, if they are it adds the No of days and does not marks any new entry to the ArrayList.
  • I was not sure if you were looking for sorting of the List too, thus did not try that.

Upvotes: 1

neel.1708
neel.1708

Reputation: 315

I dont know what exactly you want to do there but if you want to sort it with specific criteria then You could use comparable or comparator interfaces to sort your records using your criteria in ArrayList And use collections.sort() method to sort it.

Upvotes: 0

Mark
Mark

Reputation: 5566

I suppose you create these records on your own. If you don't need any specific order of the elements you should use the HashMap and as assylias said - create country elements only when they doesn't exist. When you need to keep the order of elements (or sort them later by name etc) you can still use the ArrayList and "indexOf()" method to easily find them.

Upvotes: 0

assylias
assylias

Reputation: 328608

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.

Upvotes: 1

Related Questions