我对Comparable的实现有些不对劲

时间:2015-01-11 10:54:16

标签: java collections iterable

所以我尝试使用treeMap创建数据集合,然后我希望在使用自己的比较器时得到一个排序列表。

我的问题是Collections.sort抛出错误因为Can only iterate over an array or an instance of java.lang.Iterable但是我的extsListed是ArrayList类型,它确实是Iterable,可以在这里看到http://docs.oracle.com/javase/8/docs/api/java/util/List.html

    List<FileInfo> extsListed = new ArrayList<FileInfo>(exts.values());

    for (FileInfo info : Collections.sort(extsListed)) {
        System.out.println(info.key + ' ' + info.count + ' ' + info.size);
    }

    System.out.println(totalFound.toString() + ' ' + totalSize);
}

public static class FileInfo implements Comparable<FileInfo>{
    String key;
    Integer count;
    Long size;

    public FileInfo(String key, File file){
        count = 1;
        this.key = key;
        this.size = file.length();
    }

    public int compareTo(FileInfo other) {
        if (this.size > other.size) return 1;
        else if (this.size == other.size) {
            if (this.key.compareTo(other.key) >= 1) return 1;
            else if (this.key.equals(other.key)) return 0;
            else return -1;
        }
        else return -1;
    }
}

3 个答案:

答案 0 :(得分:6)

Collections.sort返回void。它不会为您返回已排序的集合。您可以在调用sort后简单地遍历集合:

Collections.sort(extsListed);
for (FileInfo info : extsListed) {
   ...

答案 1 :(得分:2)

在Java 8中,您可以编写

public static class FileInfo implements Comparable<FileInfo>{
    String key;
    int count; // don't use a wrapper unless you want it to be null.
    long size;

    public FileInfo(String key, File file){
        count = 1;
        this.key = key;
        this.size = file.length();
    }

    public int compareTo(FileInfo other) {
        int cmp = Integer.compare(size, other.size);
        if (cmp == 0)
            cmp = key.compareTo(other.key);
        return cmp;
    }
}

然后您可以使用

对集合进行排序
extsListed.sort();

另一种方法是根本不使用Comparable。

public static class FileInfo implements Comparable<FileInfo>{
    final String key;
    final int count; // don't use a wrapper unless you want it to be null.
    final long size;

    public FileInfo(String key, File file){
        count = 1;
        this.key = key;
        this.size = file.length();
    }
    public String getKey() { return key; }
    public long getSize() { return size; }
    public String toString() { return key + ' ' + size + ' ' + count; }
}


exts.values().stream()
              .sort(comparing(FileInfo::getSize).andThen(FileInfo::getKey))
              .forEach(System.out::println);

一些可能有用的链接

答案 2 :(得分:1)

收藏sort是一个空白。所以它没有返回值:

变化:

  List<FileInfo> extsListed = new ArrayList<FileInfo>(exts.values());

    for (FileInfo info : Collections.sort(extsListed)) {
        System.out.println(info.key + ' ' + info.count + ' ' + info.size);
    }

要:

   List<FileInfo> extsListed = new ArrayList<FileInfo>(exts.values());
   Collections.sort(extsListed);
    for (FileInfo info : extsListed) {
        System.out.println(info.key + ' ' + info.count + ' ' + info.size);
    }