java排序的接口,比较接口Java的使用 - 排序文件

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 sortOrder) {

Collections.sort(sort.arrayList, sortOrder);

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

}

the Sort-class

public class Sort implements Comparator> {

private File file;

public ArrayList arrayList = new ArrayList ();

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 n1, Sort 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 {

@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 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() {

@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 files = new ArrayList<>();

try (final DirectoryStream stream = Files.newDirectoryStream(path)) {

stream.forEach(files::add);

}

files.sort(Comparator.comparing(Path::getFileName));

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值