简体   繁体   中英

Java adding unique values to hashmap <string, string>

I made a java program that will check contents of directory and generate for each file a md5 checksum. When the program is done it will save it to a CSV file. So far the lookup of files is working perfectly except that when writing to the CSV i want to make to only add new detected files. I think the issue lies with the md5 string used as key is not correctly found. Here is an excerpt of the CSV file:

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.

and here is the code that should make sure only new ones are added:

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));
        }
    }
}

As you can see from the commented code i tried in different ways already. hashmap filemap is the current status of the folder structure.

To read the already saved CSV file is use the following code:

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");
    }
}

So when i run the program it will say that all files are new even tough they are already known in the CSV. So can anyone help to get this key string checked correctly?

As @Ben pointed out, your problem is that you use String as key when putting, but char[] when getting.

It should be something along the lines:

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);
    });
}

Since you need both key as well as value from filemap , you actually better iterate over entrySet . This will save you additional filemap.get s:

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;
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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