公共类 FileDAO 扩展 DaoBase 实现 ITreeDao {File rootDirectory = null;public FileDAO(File rootDirectory) { if(!rootDirectory.exists()){ throw new IllegalArgumentException("Directory " + rootDirectory.getAbsolutePath() + " doesn't exist"); } this.rootDirectory = rootDirectory;}protected ITreeNode readRoot(ITree tree) { tree.setRoot(readNode(this.rootDirectory)); TreeSorter.sortById(tree.getRoot()); return tree.getRoot();}protected Set readChildren(ITreeNode parentNode) { Set children = new HashSet(); File parentDir = (File) parentNode.getObject(); String[] files = parentDir.list(); if(files == null) return children; for(int i=0; i<files.length; i++){ File childFile = new File(parentDir.getAbsolutePath() + File.separator + files[i]); ITreeNode child = readNode(childFile); child.setParentId(parentNode.getId()); if(!childFile.exists()) continue; children.add(child); } // Sort here TreeSorter.sortById(parentNode); return children;}protected Set readGrandChildren(ITreeNode parentNode) { Set grandChildren = new HashSet(); Iterator children = parentNode.getChildren().iterator(); while(children.hasNext()){ ITreeNode child = (ITreeNode) children.next(); grandChildren.addAll(readChildren(child)); } return grandChildren;}protected ITreeNode readNode(File file){ if(!file.exists()) return null; ITreeNode node = null; String childType = file.isDirectory() ? "directory" : "file"; if(childType.equals("file")){ node = new TreeNode(file.getAbsolutePath(), "<a href=\"openPdf.jsp?fileName=" + file.getAbsolutePath() + "\" target=_blank>" + file.getName() + "</a>" , childType); }else{ node = new TreeNode(file.getAbsolutePath(), file.getName() , childType); } node.setObject(file); return node;}}在这段代码中,我在readGrandChildren()方法上面临一个问题。就像那里我得到日历月份的升序,但我想显示日历顺序,如 Jan、Feb、Mar.....Dec。请任何人都可以帮助我吗?
1 回答
回首忆惘然
TA贡献1847条经验 获得超11个赞
使用 TreeSet,通过实现 Comparator 接口并提供反向排序逻辑,最后使用 Collection 接口的 addAll() 方法将 HashSet 的所有元素添加到 TreeSet。
// using Comparator constructor argument of TreeSet
TreeSet < String > ts = new TreeSet < String > (new Comparator < String > () {
@Override
public int compare(String o1, String o2) {
// reverse sorting logic
return o2.compareTo(o1);
}
});
// add HashSet elements to TreeSet
ts.addAll(grandChildren);
System.out.println("\n\n\nAfter Sorting : Descending order\n");
// Iterating using Iterator
Iterator < String > ascSorting = ts.iterator();
while (ascSorting.hasNext()) {
System.out.println(ascSorting.next());
}
添加回答
举报
0/150
提交
取消