获取相同String组中ArrayList中的double值的总和

时间:2018-02-13 12:37:36

标签: java arraylist

我有一个带有两个字符串值和一个double值的ArrayList。我想在相同的字符串组中将double值相加。然后将它们分配给新列表

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;
  }

}

这是一个示例ArrayList .....

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"));

例如我想要的是

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

3 个答案:

答案 0 :(得分:1)

您可以使用startActivityForResult( AuthUI.getInstance() .createSignInIntentBuilder() .setAvailableProviders( Arrays.asList( new AuthUI.IdpConfig.PhoneBuilder().alsoAddUsername().build())) .setLogo(R.drawable.logo) .build(), SIGN_IN_REQUEST_CODE ); 收集具有相同MapDashboardOneData的{​​{1}}个实例,

例如:

diviName

这将创建一个mLevel,其中键的格式为“Bm”,相应的值为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()))); 个实例,其Map成员包含DashboardOneData的总和具有相同fullHoursfullHours的所有输入DashboardOneData个实例。

要将diviName的值添加到新mLevel,您需要做的就是:

Map

答案 1 :(得分:1)

使用地图的最佳位置

func updateDepthA() {
    for item in subItems {
        item.depthA = depthA + 1
    }
}

var depthA: Int = 0 {
    didSet {
        updateDepthA()
    }
}

...

init(depth: Int) {
    self.depthA = 0
    updateDepthA()

    ...

使用key作为String(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属性的组合)声明一个Map,将值声明为double。如果key存在,则获取值添加新的double值。就像计算列表中的重复项一样。

答案 2 :(得分:0)

您可以使用设置它是一个不能包含重复元素的Collection。它模拟了数学集抽象。

尝试以下代码:

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);
     }
    }
}
}