有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java向hashmap添加唯一值<string,string>

我制作了一个java程序,它将检查目录的内容,并为每个文件生成一个md5校验和。程序完成后,它会将其保存到CSV文件中。到目前为止,文件的查找工作正常,除了在写入CSV时,我只想添加新检测到的文件。我认为问题在于没有正确找到用作键的md5字符串。 以下是CSV文件的摘录:

4d1954a6d4e99cacc57beef94c80f994,uiautomationcoreapi.h;E:\Tools\Strawberry-perl-5.24.1.1-64\c\x86_64-w64-mingw32\include\uiautomationcoreapi.h;N/A 56ab7135e96627b90afca89199f2c708,winerror.h;E:\Tools\Strawberry-perl-5.24.1.1-64\c\x86_64-w64-mingw32\include\winerror.h;N/A 146e5c5e51cc51ecf8d5cd5a6fbfc0a1,msimcsdk.h;E:\Tools\Strawberry-perl-5.24.1.1-64\c\x86_64-w64-mingw32\include\msimcsdk.h;N/A e0c43f92a1e89ddfdc2d1493fe179646,X509.pm;E:\Tools\Strawberry-perl-5.24.1.1-64\perl\vendor\lib\Crypt\OpenSSL\X509.pm;N/A As you can see first is the MD5 as key and afterwards is a long string containing name, location and score that will be split with the ; character.

下面是确保只添加新代码的代码:

private static HashMap<String, String> map = new HashMap<String,String>(); 
public void UpdateCSV(HashMap<String, String> filemap) {
    /*Set set = filemap.entrySet();
    Iterator iterator = set.iterator();
    while(iterator.hasNext()) {
        Map.Entry mentry = (Map.Entry) iterator.next();
        String md = map.get(mentry.getKey());
        System.out.println("checking key:" + md);
        if (md == null) {
            String[] line = mentry.getValue().toString().split(";");
            System.out.println("Adding new File:" + line[0]);
            map.put(mentry.getKey().toString(), mentry.getValue().toString());
        }
    }*/
    for (final String key : filemap.keySet()) {
        String md = map.get(key.toCharArray());
        if (md == null) {
            System.out.println("Key was not found:" + key);
            String[] line = filemap.get(key).toString().split(";");
            System.out.println("Adding new File:" + line[0]);
            map.put(key, filemap.get(key));
        }
    }
}

正如你从注释代码中看到的,我已经用不同的方式尝试过了。hashmap filemap是文件夹结构的当前状态

要读取已保存的CSV文件,请使用以下代码:

private void readCSV() {
    System.out.println("Reading CSV file");
    BufferedReader br = new BufferedReader(filereader);
    String line =  null;
    try {
        while ((line = br.readLine()) != null) {
            String str[] = line.split(",");
            for (int i = 0; i < str.length; i++) {
                String arr[] = str[i].split(":");
                map.put(arr[0], arr[1]);
                System.out.println("just added to map" + arr[0].toString() + " with value "+ arr[0].toString() );
            }
        }
    }
    catch(java.io.IOException e) {
        System.out.println("Can't read file");
    }
}

所以当我运行这个程序时,它会说所有的文件都是新的,即使它们在CSV中已经是已知的。那么,有人能帮助正确检查这个密钥字符串吗


共 (1) 个答案

  1. # 1 楼答案

    正如@Ben所指出的,你的问题是,你在放置时使用String作为键,而在获取时使用char[]

    应该是这样的:

    for (final String key : filemap.keySet()) {
        map.computeIfAbsent(key, k -> {
            System.out.println("Key was not found:" + k);
            String[] line = filemap.get(k).toString().split(";");
            System.out.println("Adding new File:" + line[0]);
            return filemap.get(k);
        });
    }
    

    由于需要filemap中的键和值,实际上最好在entrySet上迭代。这将为您节省额外的filemap.get

    for (final Map.Entry<String, String> entry : filemap.entrySet()) {
        final String key = entry.getKey();
        final String value = entry.getValue();
        map.computeIfAbsent(key, k -> {
            System.out.println("Key was not found:" + k);
            String[] line = value.split(";");
            System.out.println("Adding new File:" + line[0]);
            return value;
        });
    }