从arraylist和hashmap中删除重复项 [英] Remove duplicates items from arraylist and hashmap

查看:149
本文介绍了从arraylist和hashmap中删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列出谁付款的名字的arraylist,还有另一个列出每次付款费用的arraylist.例如:

I have an arraylist with the name of who pay something, and another arraylist with the cost of each payment. For example:

  • nameArray = Nicola,Raul,Lorenzo,Raul,Raul,Lorenzo,Nicola
  • priceArray = 24、12、22、18、5、8、1

我需要汇总每个人的费用.因此,数组必须变为:

I need to sum the cost of each person. So the array must become:

  • nameArray =尼古拉,劳尔,洛伦佐

  • nameArray = Nicola, Raul, Lorenzo

价格数组= 25、35、30

price Array = 25, 35, 30

然后,按价格对数组进行排序,所以:

And then, ordering the array by price, so:

nameArray =劳尔,洛伦佐,尼古拉

nameArray = Raul, Lorenzo, Nicola

priceArray = 35、30、25

priceArray = 35, 30, 25

我正在使用地图,但是现在的问题是我看到每个人的名字多次,并且每次付款都加上总和. 那是代码:

I'm using a Map, but the problem now is that I see multiple times the name of each person and each payment with the sum. That's the code:

public void bubble_sort(ArrayList<String> nameArray, ArrayList<BigDecimal> priceArray) {
    Map<String, BigDecimal> totals = new HashMap<>();

    for (int i = 0; i < nameArray.size(); ++i) {
        String name = nameArray.get(i);
        BigDecimal price = priceArray.get(i);

        BigDecimal total = totals.get(name);

        if (total != null) {
            totals.put(name, total.add(price));
        } else {
            totals.put(name, price);
        }
    }
    for (Map.Entry<String, BigDecimal> entry : totals.entrySet()) {
        nameArray.add(entry.getKey());
        priceArray.add(entry.getValue());
    }

    for (int i = 0; i < priceArray.size(); i++) {
        for (int j = 0; j < priceArray.size() - 1; j++) {
            if (priceArray.get(j).compareTo(priceArray.get(j + 1)) < 0) {
                BigDecimal tempPrice = priceArray.get(j);
                String tempName = nameArray.get(j);
                priceArray.set(j, priceArray.get(j + 1));
                nameArray.set(j, nameArray.get(j + 1));
                priceArray.set(j + 1, tempPrice);
                nameArray.set(j + 1, tempName);
            }

        }

    }
    Log.v("New nameArray", nameArray.toString());
    Log.v("New priceArray", priceArray.toString());

}

这是日志的输出:

New nameArray: [Nico, Nico, Raul, Nico, Raul, Lorenzo, Lorenzo, Raul]
New priceArray: [43.50, 25.50, 18.98, 18.00, 16.98, 9.50, 9.50, 2.00]

尼科(Nico)支付18.00 + 25.50 = 43.50,劳尔16.98 +2 = 18.98,洛伦佐9.50. 名称和价格由用户动态地插入.

Nico paid 18.00 + 25.50 = 43.50, Raul 16.98 +2 = 18.98 and lorenzo 9.50. The name and the price were insert by the user dinamically.

我需要这样显示数组:

  • nameArray:Nico,Raul,Lorenzo
  • priceArray:43.50、16.98、9.50

推荐答案

您正在将Map的条目添加到原始List中.您应该先清除它们:

You are adding the entries of the Map to the original Lists. You should clear them first:

nameArray.clear();
priceArray.clear();
for (Map.Entry<String, BigDecimal> entry : totals.entrySet()) {
    nameArray.add(entry.getKey());
    priceArray.add(entry.getValue());
}

或者,如果您不想覆盖原始的List,则应创建新的ArrayList.

Or, if you don't want to overwrite the original Lists, you should create new ArrayLists.

这篇关于从arraylist和hashmap中删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆