获取没有重复项的第二个数组列表,并将重复单元添加到单个数组中

时间:2016-02-06 04:35:09

标签: java arraylist

package com.mkyong.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

public class ReadFromCVS {

    static ArrayList<datas> dataList = new ArrayList<datas>();

    public static void main(String[] args) {

        ReadFromCVS obj = new ReadFromCVS();
        obj.run();
        obj.printDataList(ReadFromCVS.dataList);

    }


    public void run() {
        String csvFile = "C:\\Users\\User\\Downloads\\SalesData.csv";
        BufferedReader br = null;
        String csvSplitBy = ",";
        String line = "";

        try {
            br = new BufferedReader(new FileReader(csvFile));
            br.readLine();
            while ((line = br.readLine()) != null) {

                // split on comma
                String[] datas = line.split(csvSplitBy);            

                // crate data object to store values
                datas dataObject = new datas();

                // add values from the csv file to data object
                dataObject.setProductId(Integer.parseInt(datas[0]));
                dataObject.setUnits(Integer.parseInt(datas[1]));

                // adding data object to a list
                dataList.add(dataObject);
            }
            // print values stored in a datalist
            Collections.sort(dataList);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void printDataList(List<datas> dataListToPrint) {
        for (int i = 0; i < dataListToPrint.size(); i++) {
            System.out.println("Product ID: " + dataListToPrint.get(i).getProductId() + " Units: "
                    + dataListToPrint.get(i).getUnits());
        }
    }
}


class datas implements Comparable<datas> {
  private int productId;
  private int units;

  public int getProductId() {
    return productId;
  }

  public void setProductId(int productId) {
    this.productId = productId;
  }

  public int getUnits() {
    return units;
  }

  public void setUnits(int units) {
    this.units = units;
  }

  @Override
  public int compareTo(datas d) {
    if (this.getProductId() > d.getProductId()) {
        return 1;
    } else if (this.getProductId() < d.getProductId()) {
        return -1;
    } else {
        return 0;
    }
 }

 }

我想将对象从一个数组列表移动到另一个没有重复项的列表,但是当我需要将重复产品ID中的单位添加到单个产品ID

这是排序第一个arraylist后得到的一些东西:

Product ID: 10001 Units: 5
Product ID: 10001 Units: 8
Product ID: 10001 Units: 7
Product ID: 10002 Units: 4
Product ID: 10002 Units: 10
Product ID: 10002 Units: 6
Product ID: 10003 Units: 8
Product ID: 10003 Units: 6
Product ID: 10003 Units: 3
Product ID: 10003 Units: 7
Product ID: 10003 Units: 4
Product ID: 10004 Units: 6
Product ID: 10004 Units: 9
Product ID: 10004 Units: 3
Product ID: 10004 Units: 7

2 个答案:

答案 0 :(得分:0)

而不是List,使用Map可能会更容易。在解析输入文件时,您可以将产品存储在HashMap<Integer,datas> productId之内。当您从输入中处理datas的新实例时,请检查地图以查看您是否已拥有productId,如果是,请更新该实例的units

Map<Integer,datas> products = new HashMap<>();
...
// Parse your input file. For each record:
int productId = Integer.parseInt(record[0]);
int units = Integer.parseInt(record[1]);

if (products.containsKey(productId)) {
    datas existingRecord = products.get(productId);
    existingRecord.setUnits(existingRecord.getUnits() + units);
} else {
    datas d = new datas();
    d.setProductId(productId);
    d.setUnits(units);
    products.put(productId, d);
}

顺便说一句,datas作为类名不符合Java约定。将第一个字母大写,并尝试将其命名为更有意义的内容。也许Product

答案 1 :(得分:0)

为什么不创建Map?在for循环中:

map.put(Integer.parseInt(datas[0]), map.getOrDefault(Integer.parseInt(datas[0]), Integer.parseInt(datas[1])) + 1);

然后在for循环

之后
map.entrySet().stream()
            .map(o -> new datas(o.getKey(), o.getValue()))
            .collect(Collectors.toList())