简体   繁体   English

HashMap和ArrayList值的总和(来自CSV)

[英]Sum HashMap, ArrayList values (from CSV)

I'm trying to make a java program where I first read the lines from csv file then add the second column to hashmap and the third column is the value. 我正在尝试制作一个Java程序,首先读取csv文件中的行,然后将第二列添加到hashmap中,第三列是值。

There are duplicate product_id's (second column) in the file which have different values so I have added the values to array list. 文件中有重复的product_id(第二列),它们具有不同的值,因此我将这些值添加到了数组列表中。 Now i have to figure out how to actually sum these values from arraylist and then write the output to csv file (new). 现在,我必须弄清楚如何从arraylist中实际求和,然后将输出写入csv文件(新)。

Input CSV (just the beginning of file): 输入CSV(仅文件的开头):

466152;Product_69105;1
466152;Product_69133;6
466152;Product_69214;2
466152;Product_69216;2
466154;Product_01007;1
466154;Product_01083;1
466154;Product_01136;1
466154;Product_01155;1
466154;Product_01488;1
466154;Product_01491;1
466154;Product_01499;2
466154;Product_01513;1
466154;Product_01593;1
466154;Product_01646;1
466154;Product_01656;1
466154;Product_01688;1
466154;Product_01703;1
466154;Product_01770;1
466154;Product_01804;1
466154;Product_01909;1

..... .....

My output now: 我现在的输出:

Product_03067 [2, 2, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 2]
Product_82332 [1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 3, 2, 1, 2, 1, 1, 2, 1, 1]
Product_69594 [1, 1, 1, 1, 1, 1, 2, 2, 1, 1]
Product_82330 [1, 1, 1, 1, 3]
Product_03618 [1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 1, 2, 2, 2, 3, 1, 1, 1, 1, 
2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2]

.... ....

Wanted output: 想要的输出:

Product_03067, 20
Product_82332, 29
Product_69594, 12
Product_82330, 7

..... .....

Public class Tilausmäärä {

Map<String, List<Integer>> tuotteet;
String Tilausrivit = "tilausrivit.csv";
String Tilausrivit_2 = "abc.csv";

public Tilausmäärä() throws FileNotFoundException, IOException {
    tuotteet = new HashMap<>();
    readTilausrivitFile();
}

  public void readTilausrivitFile() throws FileNotFoundException, IOException {

    BufferedReader in = new BufferedReader(new FileReader(Tilausrivit));
    String line;

    while ((line = in.readLine()) != null) {
        String columns[] = line.split(";");
        String key = columns[1];
        int valueInt;
        List<Integer> valueList = null;

        try {
            valueInt = Integer.parseInt(columns[2]);
        } catch (NumberFormatException e) {
            System.out.println(e);
            continue;
        }

        if (tuotteet.containsKey(key)) {
            valueList = tuotteet.get(key);
        } else {
            valueList = new ArrayList<>();
            tuotteet.put(key, valueList );    
        }
        valueList.add(valueInt);

    } 

    in.close();
}

public static void main(String[] args) {
    try {
        Tilausmäärä tm = new Tilausmäärä();
        for (String k : tm.tuotteet.keySet()) {
            System.out.println(k + " " + tm.tuotteet.get(k));
        }


    } catch (IOException e) {
        e.printStackTrace();
    }
}

} }

Could use some help so any good ideas appreciated. 可以使用一些帮助,以便对任何好的想法表示赞赏。

Thanks! 谢谢!

There are duplicate product_id's (second column) in the file which have different values so I have added the values to array list. 文件中有重复的product_id(第二列),它们具有不同的值,因此我将这些值添加到了数组列表中。

You could direct sum the values with the same product id. 您可以直接将具有相同产品ID的值相加。
The lists you are using don't seem required. 您正在使用的列表似乎不是必需的。

Your tuotteet map that is probably declared as Map<String, List<Integer>> could be Map<String, Integer> where the Integer value would be the sum. 您的tuotteet映射可能声明为Map<String, List<Integer>>可以是Map<String, Integer> ,其中Integer值将是总和。

The actual code where you populate the ArrayList : 填充ArrayList的实际代码:

 if (tuotteet.containsKey(key)) {
     valueList = tuotteet.get(key);
 } else {
     valueList = new ArrayList<>();
     tuotteet.put(key, valueList );    
 }
 valueList.add(valueInt);

could be : 可能 :

  Integer actualValue = tuotteet.get(key);
  if (actualValue == null){
      tuotteet.put(key, valueInt);    
  }    
  else {
      tuotteet.put(key, actualValue + valueInt);    
  }

From what I understand from your code (a part of it is missing), you should have the total number as a value in the tuotteet map (whose definition is missing from the code you posted) and not the list. 根据我对您的代码的了解(部分缺失),您应该将总数作为tuotteet映射中的值(发布的代码中缺少其定义)而不是列表。 If the element is already in the map, you should increment its value, otherwise you should set its value to the current value. 如果元素已经在地图中,则应增加其值,否则应将其值设置为当前值。

Something like this: 像这样:

Map<String, Integer> tuotteet;
...
    try {
        valueInt = Integer.parseInt(columns[2]);
    } catch (NumberFormatException e) {
        System.out.println(e);
        continue;
    }

    Integer val = null;
    if (tuotteet.containsKey(key)) {
        val = tuotteet.get(key) + valueInt;
    } else {
        val = valueInt;
    }
    tuotteet.put(key, val );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM