Wednesday 15 July 2015

java - More efficient sorting for pairs of files -



java - More efficient sorting for pairs of files -

i have folder pairs of files. pairs share filename have different extension (in case, pairs of .txt , .png). looking store them hashmap< file, file > pairs. here did find pairs:

linkedlist<file> filelist = new linkedlist<file>(arrays.aslist(filearray)); linkedlist<file> alreadycompared = new linkedlist<file>(); hashmap<file, file> filepairs = new hashmap<file, file>(); (iterator<file> itr1 = filelist.iterator(); itr1.hasnext(); ) { file comparator = itr1.next(); if (!alreadycompared.contains(comparator)) { string stringcomparator = comparator.getname().split("\\.")[0]; alreadycompared.add(comparator); (iterator<file> itr2 = filelist.iterator(); itr2.hasnext(); ) { file compared = itr2.next(); if (!alreadycompared.contains(compared)) { string stringcompared = compared.getname().split("\\.")[0]; if (stringcomparator.equals(stringcompared)) { if (comparator.getname().endswith("txt")) { filepairs.put(comparator, compared); } else { filepairs.put(compared, comparator); } } } } } } homecoming filepairs;

now, takes a lot of time when have more 1000 files sort, , i'd find more efficient way of doing it. other way can sort these files?

thanks lot!

instead of putting them in unordered map hashmap, set elements in treemap because sorts it's keys.

per linked javadoc,

a red-black tree based navigablemap implementation. map sorted according natural ordering of keys, or comparator provided @ map creation time, depending on constructor used.

java sorting

No comments:

Post a Comment