There isn't a more efficient one. The algorithm will have to test each and every file if it's extension is one, that hasn't been seen before. So the best algorithm will have a complexity of O(n)
.
Recursing into all directories and throwing the exstensions of all files in a Set
is the best we can do, to my opinion.
The dramatic performance gain may be a side effect of not useing a HashMap
correctly ;) I see, that you iterate through the entire set instead of using the contains method. If you did that in your original version true, then it's cleear to me, that the performance was questionable.
I still expect, that extracting the extensions and just adding them to a HashSet
is the most performant solution:
static String[] filenames = { "edit.txt", "my.notes.txt", "sheet.xlxs",
".bash", "README" };
static HashSet<String> exts = new HashSet<>();
public static void main(String[] args) {
// we add every extension to a hashset
for (String filename : filenames) {
exts.add(getExtension(filename));
}
// just dumps the set contents
for (String ext: exts) {
System.out.println(ext);
}
}
private static String getExtension(String filename) {
String ext = "";
// calculate the index only once
int lastIndexOfDot = filename.lastIndexOf('.');
// "README" and ".bash" are files with no extension!
if (lastIndexOfDot > 0) {
exts.add(filename.substring(lastIndexOfDot));
}
return ext;
}