【问题标题】:Something's wrong with my implementation of Comparable我的 Comparable 实现出了点问题
【发布时间】:2025-11-25 15:40:02
【问题描述】:

所以我尝试使用 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;
    }
}

【问题讨论】:

    标签: java collections iterable


    【解决方案1】:

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

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

    【讨论】:

    • @DominikDitoIvosevic 你可以调试并找出原因。
    【解决方案2】:

    集合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);
        }
    

    【讨论】:

      【解决方案3】:

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

      一些可能有用的链接

      【讨论】:

      • 呜呜呜我喜欢这个。我的问题一直是为什么没有这样的东西。 +50
      • @DominikDitoIvosevic 顺便说一句,如果您不使用 Java 8,也许您应该这样做。 Java 7 将于 4 月 EOL。
      • 我有大约 15 小时的 Java 编码,所以它不像“不使用 java8”,而更多的是“不使用任何 java”:D 你能给我一些我应该使用的谷歌搜索词吗了解更多关于 sort 的功能用法
      • @DominikDitoIvosevic 我添加了一些关于主题的链接。如果您想了解更多信息,可以从中找到一些术语。
      • @DominikDitoIvosevic 对我来说,这是 Java 的 15 年;)
      最近更新 更多