0

For example, I've got some place in my code, that receives many files (many of them are identical) from disk and, further, unmarshalls them.

final File configurationFile = getConfigurationFile();
FileOutputStream fileOutputStream = new FileOutputStream(configurationFile);
Marshaller.marshal(configObject, fileOutputStream);

Obviously, I can create a special cache map for them to increase performance (in order not to unmarshall identical files again and again). For my case, HashMap implementation will be enough.

The question is: what key for that should I use?

configurationFile.hashCode() is very bad for this?

Thanks for all your answers!

Developer87
  • 2,448
  • 4
  • 23
  • 43
  • 2
    How do you identify that two files are identical? By file name? If the answer is yes, file name should be the key. – Eran Oct 01 '14 at 08:57
  • Anyway, using hashCode would be wrong, since different files may have the same hashCode. – Eran Oct 01 '14 at 09:00

3 Answers3

0

You can also use hashset without actually worrying about key.

if(hashset.add(file)) {
   // do unmarshling;
} else {
   //do nothing
}

Hashset.add() method return true if an object can be added. If you try to add duplicate entry then it will return false since duplicacy is not allowed in sets.

Paras Mittal
  • 1,159
  • 8
  • 18
0

...identical files again and again...

What is identical?

  • If the file content decides, you may use a hash of the file content (e.g. MD5, SHA1, SHA256) as the key.

  • If the file name must be identical, simply use the file name as the key.

  • If the file path, then use the full path of the file as the key (File.getCanonicalPath()).

icza
  • 389,944
  • 63
  • 907
  • 827
0

Use the canonical path instead of the absolute path (explanation of the difference) and put it in a HashSet. Sets don't allow duplicated values. If you try to add a value that already exists, it will return false, otherwise true.

Example code (untested):

Set<String> filesMarshalled= new HashSet<>();
...
final File configurationFile = getConfigurationFile();
if (filesMarshalled.add(configurationFile.getCanonicalPath())) {
    //not marshalled yet
    FileOutputStream fileOutputStream = new FileOutputStream(configurationFile);
    Marshaller.marshal(configObject, fileOutputStream);
}
Community
  • 1
  • 1
Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76