比较接口Java的使用 - 排序文件 [英] Use of Comparator interface Java - Sort files

查看:124
本文介绍了比较接口Java的使用 - 排序文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


$ b $ 1)如果有人能说出我如何使用compare-method(原型是:sort(List list,Comparator c)),那么会很高兴

2)如何在目录类中获得输出结果?由于Sort-class是参数化的,我无法访问该方法 public String getName()

class创建类Sort和把它们放在Arraylist(Sort的成员)中。

  private Sort sort = new Sort(); 
file = new File(System.getProperty(dir));
File [] files = getFiles(); //只返回文件,而不是目录;

for(int i = 0; i< files.length; i ++){
sort.arrayList.add(new Sort(files [i])); //将它们放入属于


列表(排序)的ArrayList中; //调用list-method



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

//现在 - 如何从这里获取排序的文件名?
}

Sort-class

  public class Sort< File>实现比较器< Sort< File>> {

私有文件文件;
public ArrayList< Sort> arrayList = new ArrayList< Sort> ();


公共排序(文件文件){
this.file = file;

$ b $ public Sort(){

}

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

//回调方法。在另一个类中调用Collections.sort()时使用。
public int compare(Sort< File> n1,Sort< File> n2){
//如何对文件名上的对象进行排序。


解决方案

希望 Comparator 比较 File 然后告诉它

  public class Sort implements Comparator< File> {

@Override
public int compare(File n1,File n2){
return n1.getName()。compareTo(n2.getName);
}

}

您要求它比较实例本身。并且声明 Sort< File> 告诉编译器你需要一个 generic class 其中泛型类型参数碰巧称为 File 。这与 File class 无关。



为了使用这个比较器,你需要做的是:

  final File file = new File(System.getProperty(dir)); 
final File [] files = file.listFiles();
Arrays.sort(文件,新的Sort());
for(final File f:files){
//用f
做某事}

或者更好的是,只需使用匿名 class ,这会阻止你对 Comparator 做任何奇怪的事情:

  Arrays.sort(文件,新比较器< File>(){
@Override
public int compare(File o1,File o2) {
return o1.getName()。compareTo(o2.getName());
}
});

但是,如果您使用的是Java 8,则可以完全跳过这个混乱情况:

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

现在您有一个已排序的 List< Path> ,你可以随心所欲地做任何事情。例如,要将排序后的列表打印到控制台:

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


I have a program to sort files in a certain directory of the computer. I am using the Comparator-interface and using the collections.sort-method but I cannot access the output - from the calling-class. I neither know how to sort the objects in the Sort-class either.

1) Would be glad if someone could tell how I use the compare-method (prototyp is: sort(List list, Comparator c)

2) How do I get the output in the directory class? Because Sort-class is parameterisized I cannot access the method public String getName()

class Directory that creates object of class Sort and put those in an Arraylist (member of 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?
}

the Sort-class

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.
}    

解决方案

First things first, if you want the Comparator to compare File then tell it that:

public class Sort implements Comparator<File> {

    @Override
    public int compare(File n1, File n2){
        return n1.getName().compareTo(n2.getName);
    }    

}

You are asking it to compare instances of itself. And the declaration Sort<File> tells the compiler that you want a generic class where the generic type parameter happens to be called File. This has nothing to do with the File class.

In order to use this Comparator all you need to do is:

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
}

Or better yet, simply use an anonymous class, this will prevent you from doing anything odd with the Comparator:

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

But, if you are using Java 8, you can skip this mess entirely:

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));

Now you have a sorted List<Path>, you can do whatever you want with it. For example to print the sorted list to the console:

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

这篇关于比较接口Java的使用 - 排序文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆