猿问

java中的日历顺序

公共类 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。


请任何人都可以帮助我吗?




达令说
浏览 134回答 1
1回答

回首忆惘然

使用 TreeSet,通过实现 Comparator 接口并提供反向排序逻辑,最后使用 Collection 接口的 addAll() 方法将 HashSet 的所有元素添加到 TreeSet。// using Comparator constructor argument of TreeSetTreeSet < String > ts = new TreeSet < String > (new Comparator < String > () {&nbsp;@Override&nbsp;public int compare(String o1, String o2) {&nbsp; // reverse sorting logic&nbsp; return o2.compareTo(o1);&nbsp;}});// add HashSet elements to TreeSetts.addAll(grandChildren);System.out.println("\n\n\nAfter Sorting : Descending order\n");// Iterating using IteratorIterator < String > ascSorting = ts.iterator();while (ascSorting.hasNext()) {&nbsp;System.out.println(ascSorting.next());}
随时随地看视频慕课网APP

相关分类

Java
我要回答