Get the sum of the double values ​​in an ArrayList within the same String group

I have an ArrayList with two string values ​​and a double value. I want to sum the double value in the same string groups. Then assign them to the new list

public class DashboardOneData {

    private String diviName;
    private double fullHours;
    private String mLevel;

public DashboardOneData(String x, double y, String z){
    this.diviName= z;
    this.fullHours= y;
    this.mLevel= y;
  }

}

This is an ArrayList sample .....

List<DashboardOneData > items = new ArrayList<DashboardOneData >();
items.add(new DashboardOneData ("A", 1.11 , "x"));
items.add(new DashboardOneData ("A", 1.52 , "x"));
items.add(new DashboardOneData ("B", 2.22 , "m"));
items.add(new DashboardOneData ("A", 3.12 , "m"));
items.add(new DashboardOneData ("B", 2.52 , "m"));
items.add(new DashboardOneData ("C", 2.85 , "y"));
items.add(new DashboardOneData ("A", 2.22 , "y"));
items.add(new DashboardOneData ("A", 3.12 , "y"));
items.add(new DashboardOneData ("B", 1.01 , "m"));
items.add(new DashboardOneData ("C", 2.85 , "y"));

For example, I want

sum String group A, x(double value)
sum String group A, m(double value)
sum String group A, y(double value)
sum String group B, x(double value)
sum String group B, m(double value)
sum String group B, y(double value)


A-x = 2.63
A-m = 3.12
B-m = 5.75
C-y = 5.70
+4
source share
3 answers

You can use Mapto collect instances DashboardOneDatawith the same diviNameand mLevel,

For instance:

Map<String,DashboardOneData>
    map = items.stream()
               .collect(Collectors.toMap(d -> d.getDiviName() + "-" + d.getLevel(),
                                         Function.identity(),
                                         (d1,d2) -> new DashboardOneData (d1.getDiviName(),d1.getHours()+d2.getHours(),d1.getLevel())));

This will create Mapwhere the key is in the form of "Bm" and the corresponding value is an instance DashboardOneDatawhose member fullHourscontains the sum of fullHoursall input DashboardOneDatainstances having the same diviNameand mLevel.

Map List, , , :

List<DashboardOneData> list = new ArrayList<>(map.values());
+1

class DashboardOneData {
    private String diviName;
    private double fullHours;
    private String mLevel;
    public String getDiviName() {
        return diviName;
    }
    public void setDiviName(String diviName) {
        this.diviName = diviName;
    }
    public double getFullHours() {
        return fullHours;
    }
    public void setFullHours(double fullHours) {
        this.fullHours = fullHours;
    }
    public String getmLevel() {
        return mLevel;
    }
    public void setmLevel(String mLevel) {
        this.mLevel = mLevel;
    }
    public DashboardOneData(String diviName, double fullHours, String mLevel) {
        super();
        this.diviName = diviName;
        this.fullHours = fullHours;
        this.mLevel = mLevel;
    }
}

public class AddListItemsBasedOnAttributes {

    public static void main(String[] args) {
        Map<String, Double> sumMap = new TreeMap<String, Double>();

        List<DashboardOneData> items = new ArrayList<DashboardOneData >();
        items.add(new DashboardOneData ("A", 1.11 , "x"));
        items.add(new DashboardOneData ("A", 1.52 , "x"));
        items.add(new DashboardOneData ("B", 2.22 , "m"));
        items.add(new DashboardOneData ("A", 3.12 , "m"));
        items.add(new DashboardOneData ("B", 2.52 , "m"));
        items.add(new DashboardOneData ("C", 2.85 , "y"));
        items.add(new DashboardOneData ("A", 2.22 , "y"));
        items.add(new DashboardOneData ("A", 3.12 , "y"));
        items.add(new DashboardOneData ("B", 1.01 , "m"));
        items.add(new DashboardOneData ("C", 2.85 , "y"));

        Iterator<DashboardOneData> it = items.iterator();
        while(it.hasNext()) {
            DashboardOneData dashboardOneData = it.next();
            String key = dashboardOneData.getDiviName() + "-" + dashboardOneData.getmLevel();
            if(sumMap.containsKey(key)) {
                double sum = sumMap.get(key);
                sum += dashboardOneData.getFullHours();
                sumMap.put(key, sum);
            }
            else {
                sumMap.put(key, dashboardOneData.getFullHours());
            }
        }
        System.out.println(sumMap);
    }   
}

String ( String DashboardOneData) double. , . , .

+1

You can use the Set collection, which cannot contain duplicate elements. He models a mathematical abstract abstraction.

Try entering the code:

Set<DashboardOneData> newItems = new HashSet<DashboardOneData>();

for (DashboardOneData dashboardOneData : items) {
int double sum =0;
 for (int i = 0; i < items.size(); i++) {
    if(dashboardOneData.diviName.equals(items.get(i).diviName) && dashboardOneData.mLevel.equals(items.get(i).mLevel) ){
    sum=sum+dashboardOneData.fullHours;
    if(i==items.size()-1){
        newItems.add(dashboardOneData);
     }
    }
}
}
0
source

Source: https://habr.com/ru/post/1693573/


All Articles