【问题标题】:Use of Comparator interface Java - Sort files使用 Comparator 接口 Java - 对文件进行排序
【发布时间】:2015-02-21 10:02:27
【问题描述】:

我有一个程序可以对计算机某个目录中的文件进行排序。我正在使用 Comparator-interface 并使用 collections.sort-method 但我无法访问输出 - 从调用类。我也不知道如何对 Sort-class 中的对象进行排序。

1) 如果有人能告诉我如何使用比较方法会很高兴(原型是:sort(List list, Comparator c)

2) 如何获取目录类中的输出?因为排序类是参数化的,所以我无法访问方法 public String getName()

class 目录,创建 Sort 类的对象并将其放入 Arraylist(Sort 的成员)

 private Sort sort = new Sort();
 file = new File(System.getProperty(dir));
 File[] files = getFiles(); // return only files, not directories;

 for (int i = 0; i < files.length; i++) {
    sort.arrayList.add(new Sort(files[i])); // put those in an ArrayList belonging to sort
 }

list(sort); // call list-method



public void list(Comparator<File> sortOrder) {
    Collections.sort(sort.arrayList, sortOrder);

// now - how do I get the sorted fileNames from here?
}

排序类

public class Sort<File> implements Comparator<Sort<File>> {

private File file;
public ArrayList <Sort> arrayList = new ArrayList <Sort> ();


public Sort(File file) {
    this.file = file;
}

public Sort() {

}

public String getName() {
    return this.file.toString();
}

// callback-method. Used when calling Collections.sort() in the other class.
public int compare(Sort<File> n1, Sort<File> n2){
 // how do I sort objects on Filesnames.
}    

【问题讨论】:

    标签: java generics comparator


    【解决方案1】:

    首先,如果您想让ComparatorFile 进行比较,请告诉它

    public class Sort implements Comparator<File> {
    
        @Override
        public int compare(File n1, File n2){
            return n1.getName().compareTo(n2.getName);
        }    
    
    }
    

    您要求它比较自身的实例。声明Sort&lt;File&gt; 告诉编译器你想要一个generic class,其中泛型类型参数恰好被称为File。这与Fileclass无关。

    要使用这个Comparator,您只需:

    final File file = new File(System.getProperty(dir));
    final File[] files = file.listFiles();
    Arrays.sort(files, new Sort());
    for(final File f : files) {
        //do something with f
    }
    

    或者更好的是,只需使用anonymous class,这将防止您对Comparator 做任何奇怪的事情:

    Arrays.sort(files, new Comparator<File>() {
        @Override
        public int compare(File o1, File o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
    

    但是,如果您使用的是 Java 8,则可以完全跳过这个烂摊子:

    final Path path = Paths.get(System.getProperty(dir));
    final List<Path> files = new ArrayList<>();
    try (final DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
        stream.forEach(files::add);
    }
    files.sort(Comparator.comparing(Path::getFileName));
    

    现在你有一个排序的List&lt;Path&gt;,你可以用它做任何你想做的事情。例如将排序后的列表打印到控制台:

    files.forEach(System.out::println);
    

    【讨论】:

    • +1 为您解答 - 但如果我仍然需要使用列表方法,即 public void list(Comparatator sortOrder) 怎么办?
    • list 方法有什么作用?它只需要Comparator,没有什么可比较的......
    • 我应该提到这个列表来自一个接口——所以它必须被实现。它只是应该从 Sort 接收一个对象,然后使用该对象调用 collections.sort。那就是 list(sort) 。你可以在我的问题中看到它。
    【解决方案2】:

    您只需要遍历这些值并对这些值执行任何所需的任务。

    所以在你的例子中:

    public void list(Comparator<File> sortOrder) {
        Collections.sort(sort.arrayList, sortOrder);
    
    // now - how do I get the sorted fileNames from here?
       for (Sort<File> arrayList : file) {
         System.out.println("Sorted list filenames: " + file.getName());
         /* ... other actions ... */
       }
    
    }
    

    只要您实现了正确的通用comparator 方法,就应该这么简单

    【讨论】:

    • 谢谢 - 但 Eclipse 抱怨这一点 - 在 for 循环中为“文件”加下划线:只能遍历数组或 java.lang.Iterable 的实例。跨度>
    • 您错过了 OPs 代码中的所有问题。此建议无法解决任何问题。
    猜你喜欢
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    相关资源
    最近更新 更多