2015-01-11 5 views
2

그래서 treeMap을 사용하여 데이터 컬렉션을 만든 다음 내 자신의 비교기를 사용하는 동안 정렬 된 목록을 가져 오려고합니다.내 구현에 뭔가 잘못되었습니다.

내 문제는은, Collections.sort 오류 때문에 Can only iterate over an array or an instance of java.lang.Iterable을 던져 것입니다하지만 내 extsListed 여기 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; 
    } 
} 

답변

2

자바 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; 
    } 
} 

을 작성할 수 있습니다

변경 :

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

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

에 따라서는 값을 반환하지 않습니다 와 함께

extsListed.sort(); 

또 다른 접근법은 Comparable을 전혀 사용하지 않는 것입니다.

+0

우와 우우 나는 이것을 좋아한다. 내 질문에 왜 내게 이런 것이 없었는가? +50 – ditoslav

+0

@DominikDitoIvosevic 만약 당신이 Java 8을 사용하고 있지 않다면, 아마도 그렇게해야합니다. Java 7은 4 월에 EOL이 될 예정입니다. –

+0

나는 약 15 시간 동안 자바로 코딩 했으므로 "java8을 사용하지 않음"과 "자바를 사용하지 않음"이 많지 않다. D는 나에게이 기능적 사용법에 대해 좀 더 알아야 할 몇 가지 구글 검색 용어를 줄 수 있겠는가? – ditoslav

6

Collections.sort 반환 void 볼 수 있듯이 실제로의 Iterable이다 타입의 ArrayList이다. 정렬 된 컬렉션을 반환하지 않습니다. 당신은 단순히 sort를 호출 한 후 수집 자체를 반복 할 수 있습니다

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

아 남자 유용 할 수 있습니다

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

일부 링크 : 당신이 및 디버깅 수도 @DominikDitoIvosevic/감사 – ditoslav

+0

원인을 파악 . – underdog

1

컬렉션 sort은 무효입니다.

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