Java 如果使用arraylist的货币相同,则添加计数、小计

Java 如果使用arraylist的货币相同,则添加计数、小计,java,Java,下面是一个示例列表项..包括1*10=10(计数*货币=小计) 如果货币相同,我想加上计数,小计 比如说 1 * 10 = 10 3 * 10 = 30 清单检查后,最终lsit必须 4 * 10 = 40. public class CashModal { private int count; private int currency; private int subTotal; gettter/setter } listItems.ad

下面是一个示例列表项..包括1*10=10(计数*货币=小计) 如果货币相同,我想加上计数,小计

比如说

1 * 10 = 10
3 * 10 = 30
清单检查后,最终lsit必须 4 * 10 = 40.

public class CashModal {


    private int count;
    private int currency;
    private int subTotal;  
gettter/setter
}


        listItems.add("1 * 10 = 10");
        listItems.add("2 * 10 = 20");
        listItems.add("1 * 5 = 5");
        listItems.add("2 * 10 = 20");

           cashModals=new ArrayList<CashModal>();
          for(String s:listItems){
        cashModal=new CashModal();
        String x[]=s.split("\\*");
        cashModal.setCount(Integer.parseInt(x[0].trim()));
        cashModal.setCurrency(Integer.parseInt(x[1].split("\\=")[0].trim()));
        cashModal.setSubTotal(Integer.parseInt(x[1].split("\\=")[1].trim()));
        cashModals.add(cashModal);
        subTotal=subTotal+Integer.parseInt((x[1].split("\\=")[1]).trim());
            }
公共类{
私人整数计数;
私人国际货币;
私人整数小计;
接球手/接球手
}
添加(“1*10=10”);
添加(“2*10=20”);
添加(“1*5=5”);
添加(“2*10=20”);
cashModals=newarraylist();
对于(字符串s:listItems){
cashModal=新的cashModal();
字符串x[]=s.split(\\*);
cashModal.setCount(Integer.parseInt(x[0].trim());
cashModal.setCurrency(Integer.parseInt(x[1].split(“\\=”)[0].trim());
cashModal.setSubTotal(Integer.parseInt(x[1].split(“\\=”)[1].trim());
cashModals.add(cashModal);
小计=小计+整数.parseInt((x[1].split(“\\=”[1]).trim());
}

使用地图来维护数据。关键是货币字段。请尝试以下代码:

ArrayList<String> listItems = new ArrayList<String>();
        listItems.add("1 * 10 = 10");
        listItems.add("2 * 10 =  20");
        listItems.add("1 * 5 = 5");
        listItems.add("2 * 10 = 20");

        Map<Integer, CashModal> cashModalsMap = new HashMap<Integer, CashModal>();
        int subTotal = 0;
        int existingCount = 0;
        int existingSubtotal = 0;
        for (String s : listItems) {
            String x[] = s.split("\\*");
            if (!cashModalsMap
                    .containsKey(Integer.parseInt(x[1].split("\\=")[0].trim()))) {
                CashModal cashModal = new CashModal();

                cashModal.setCount(Integer.parseInt(x[0].trim()));
                cashModal.setCurrency(Integer.parseInt(x[1].split("\\=")[0]
                        .trim()));
                cashModal.setSubTotal(Integer.parseInt(x[1].split("\\=")[1]
                        .trim()));
                cashModalsMap.put(
                        Integer.parseInt(x[1].split("\\=")[0].trim()),
                        cashModal);
                subTotal = subTotal
                        + Integer.parseInt((x[1].split("\\=")[1]).trim());
            } else {
                CashModal cashModalExisting = cashModalsMap.get(Integer
                        .parseInt(x[1].split("\\=")[0].trim()));
                existingCount = cashModalExisting.getCount();
                existingSubtotal = cashModalExisting.getSubTotal();
                cashModalExisting.setCount(existingCount
                        + Integer.parseInt(x[0].trim()));
                cashModalExisting.setSubTotal(existingSubtotal
                        + Integer.parseInt(x[1].split("\\=")[1].trim()));
                cashModalsMap.put(
                        Integer.parseInt(x[1].split("\\=")[0].trim()),
                        cashModalExisting);
            }
        }
        for(CashModal cm:cashModalsMap.values()){
        System.out.println("Count:"+cm.getCount()+",Currency:"+cm.getCurrency()+",SubTotal:"+cm.getSubTotal());
        }
    }
ArrayList listItems=new ArrayList();
添加(“1*10=10”);
添加(“2*10=20”);
添加(“1*5=5”);
添加(“2*10=20”);
Map cashModalsMap=newhashmap();
整数小计=0;
int existingCount=0;
int existingSubtotal=0;
对于(字符串s:listItems){
字符串x[]=s.split(\\*);
如果(!cashModalsMap
.containsKey(整数.parseInt(x[1].split(“\\=”[0].trim())){
CashModal CashModal=新的CashModal();
cashModal.setCount(Integer.parseInt(x[0].trim());
cashModal.setCurrency(Integer.parseInt(x[1])。拆分(“\\=”)[0]
.trim());
cashModal.setSubTotal(Integer.parseInt(x[1].split(“\\=”)[1]
.trim());
cashModalsMap.put(
整数.parseInt(x[1]。拆分(“\\=”[0]。修剪()),
现金模式);
小计=小计
+整数.parseInt((x[1].split(“\\=”[1]).trim());
}否则{
CashModal cashModalExisting=cashModalsMap.get(整数
.parseInt(x[1].split(“\\=”[0].trim());
existingCount=cashModalExisting.getCount();
existingSubtotal=cashModalExisting.getSubTotal();
cashModalExisting.setCount(existingCount
+整数.parseInt(x[0].trim());
现金流量分类.设置小计(现有小计)
+整数.parseInt(x[1].split(“\\=”[1].trim());
cashModalsMap.put(
整数.parseInt(x[1]。拆分(“\\=”[0]。修剪()),
现金流动性);
}
}
对于(CashModal cm:cashModalsMap.values()){
System.out.println(“计数:+cm.getCount()+”,货币:+cm.getCurrency()+”,小计:+cm.getSubTotal());
}
}

您面临的问题是什么?我想从列表cashModals中删除相同的货币,还想增加计数,小计。