如何将重复数组元素指定为唯一键,并计算相应的整数值?

时间:2016-06-19 05:38:37

标签: java arrays file csv hashmap

我花了一段时间研究类似于我的问题,现在我的代码卡住了。我已经实现了一个基本的CSV文件阅读器,但我一直在处理输入

当前结果:

Source [medium = go bio , values = 5]
Source [medium = go bio , values = 5]
Source [medium = metal sink , values = 2]
Source [medium = go bio , values = 5]
Source [medium = metal sink , values = 3]
Done

但我想要的是这样的。

输出:

[medium = go bio , values = 15]
[medium = metal sink , values = 5]

我的文本文件的内容。

go,bio,5
go,bio,5
metal,sink,2
go,bio,5
metal,sink,3

这是我的代码:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileReader;
import java.util.HashMap;

public class CVSReader {
    public static void main(String[] args) {
        CVSReader obj = new CVSReader();
        obj.run();
    }

    public void run() {
        String csvFile = "file2.txt";
        BufferedReader br = null;
        String line;
        String cvsSplitBy = ",";
        String[] source;

        try {
            //HashMap here
            HashMap<String, String> hash = new HashMap<>();
            HashMap<String, Integer> hash2 = new HashMap<>();

            br = new BufferedReader(new FileReader(csvFile));
            while ((line = br.readLine()) != null) {
                // comma separator here
                source = line.split(cvsSplitBy);

                hash.put(source[0], source[0]);
                hash.put(source[1], source[1]);
                hash.put(source[2], source[2]);

                System.out.println("Source [medium = " + hash.get(source[0]) + " " +
                        hash.get(source[1]) + " , values = " + hash.get(source[2]) + "]");
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("Done");
    }
}

1 个答案:

答案 0 :(得分:1)

首先,您需要使用相同的密钥Map<String, Integer> 添加您的条目。构建密钥,解析line中的值,如果已经在Map中,则将当前值添加到已解析的值中。然后将值存储在Map中。我也更喜欢一个try-with-resources close。最后,迭代Map.entrySet()。像,

String csvFile = "file2.txt";
String cvsSplitBy = "\\s*,\\s*"; // <-- adds support for white space(s)
                                 //     before or after the comma.
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
    Map<String, Integer> hash = new HashMap<>();

    String line;
    while ((line = br.readLine()) != null) {
        String[] source = line.split(cvsSplitBy);
        String medium = source[0] + " " + source[1]; // <-- the key
        int val = Integer.parseInt(source[2]);       // <-- the value
        if (hash.containsKey(medium)) { // <-- is the key in Map?
            val += hash.get(medium);    // <-- yes, add the value.
        }
        hash.put(medium, val); // <--  store the value back in the Map.
    }
    // Iterate the entry set, display the "medium" and value.
    for (Map.Entry<String, Integer> entry : hash.entrySet()) {
        System.out.printf("Source [medium = %s , values = %d]%n",
                entry.getKey(), entry.getValue());
    }
} catch (IOException e) { // <-- FileNotFoundException is a sub-class.
    e.printStackTrace();
}
System.out.println("Done");

我使用您提供的文本文件运行,并获得(根据要求)

Source [medium = metal sink, values = 5]
Source [medium = go bio, values = 15]
Done