Java Collections.sort()不比较元素

时间:2012-11-11 21:39:31

标签: java sorting collections

我有一个IndexEntry的ArrayList,它是一个带字符串的简单类,下面是两个整数和一个compareTo()方法。我的问题是,当我在ArrayList上调用Collections.sort()时,大多数时候所有内容都正确排序,但有时两个元素永远不会被比较,导致列表排序错误。

一些参考代码:

//Get a list of the values
ArrayList<IndexEntry<T>> cleanEntries = new ArrayList<IndexEntry<T>>(matches.get(query).values());

//Sort Files/URLS
Collections.sort(cleanEntries);

public int compareTo(IndexEntry<T> other) {
    if(this.occurrenceCount < other.getOccurrenceCount()){
        return 1;
    }
    else if(this.occurrenceCount > other.getOccurrenceCount()){
        return -1;
    }
    else{//Need to compare Initial Positions
        if(this.firstOccurrence == other.getFirstOccurrence()){
            return this.getLocationName().compareTo(other.getLocationName());
        }
        else if(this.firstOccurrence > other.getFirstOccurrence()){
            return 1;
        }
        else{
            return -1;
        }
    }
}   

以下是字母“C”的所有IndexEntry的一些示例输出:

c
"index.html", 6, 2
"chickadee.html", 3, 2
"cuckoo.html", 3, 2
"crow.html", 3, 2
"crane.html", 3, 2
"cardinal.html", 3, 2
"quail.html", 2, 2
"robin.html", 2, 2
"finch.html", 2, 2
"kingfisher.html", 2, 2
"swan.html", 2, 2
"turkey.html", 2, 2
"ibis.html", 2, 2
"falcon.html", 2, 2
"albatross.html", 2, 2
"tern.html", 2, 2
"dove.html", 2, 2
"mockingbird.html", 2, 2
"owl.html", 2, 2
"sparrow.html", 2, 2
"hawk.html", 2, 2
"magpie.html", 2, 2
"nighthawk.html", 2, 2
"gull.html", 2, 2
"bluebird.html", 2, 2
"raven.html", 2, 2
"blackbird.html", 2, 2
"sandpiper.html", 2, 2
"pheasant.html", 2, 2
"roadrunner.html", 2, 2
"duck.html", 2, 2
"vulture.html", 2, 2
"pigeon.html", 2, 2
"heron.html", 2, 2
"stork.html", 2, 2
"warbler.html", 2, 2
"woodpecker.html", 2, 2
"hummingbird.html", 2, 2
"mallard.html", 2, 2
"osprey.html", 2, 2
"swallow.html", 2, 2
"wren.html", 2, 2
"goose.html", 2, 2
"meadowlark.html", 2, 2
"pelican.html", 2, 2
"starling.html", 2, 2
"loon.html", 2, 2
"eagle.html", 2, 2
"puffin.html", 2, 2
"egret.html", 2, 2

注意在发生计数和初始发生后确定如何相同它停止排序。正确的输出是:

c
"index.html", 6, 2
"cardinal.html", 3, 2
"chickadee.html", 3, 2
"crane.html", 3, 2
"crow.html", 3, 2
"cuckoo.html", 3, 2
"albatross.html", 2, 2
"blackbird.html", 2, 2
"bluebird.html", 2, 2
"dove.html", 2, 2
"duck.html", 2, 2
"eagle.html", 2, 2
"egret.html", 2, 2
"falcon.html", 2, 2
"finch.html", 2, 2
"goose.html", 2, 2
"gull.html", 2, 2
"hawk.html", 2, 2
"heron.html", 2, 2
"hummingbird.html", 2, 2
"ibis.html", 2, 2
"kingfisher.html", 2, 2
"loon.html", 2, 2
"magpie.html", 2, 2
"mallard.html", 2, 2
"meadowlark.html", 2, 2
"mockingbird.html", 2, 2
"nighthawk.html", 2, 2
"osprey.html", 2, 2
"owl.html", 2, 2
"pelican.html", 2, 2
"pheasant.html", 2, 2
"pigeon.html", 2, 2
"puffin.html", 2, 2
"quail.html", 2, 2
"raven.html", 2, 2
"roadrunner.html", 2, 2
"robin.html", 2, 2
"sandpiper.html", 2, 2
"sparrow.html", 2, 2
"starling.html", 2, 2
"stork.html", 2, 2
"swallow.html", 2, 2
"swan.html", 2, 2
"tern.html", 2, 2
"turkey.html", 2, 2
"vulture.html", 2, 2
"warbler.html", 2, 2
"woodpecker.html", 2, 2
"wren.html", 2, 2

无论如何,就像我说我必须省略很多信息一样,但如果有什么想法我真的很感激想法。事实上,在创建ArrayList之后,“匹配”数据结构不会被修改。

编辑: 嘿,我还在读你的回答。非常感谢您的快速帮助!以下是整个IndexEntry类,因为你们中的一些人问:

    class IndexEntry<extends Comparable<T>> implements Comparable<IndexEntry<T>>{
        private T theLocation;
        private Integer occurrenceCount;
        private Integer firstOccurrence;

        public IndexEntry(T location, Integer firstOcc, Integer occCount){
            theLocation = location;
            occurrenceCount = occCount;
            firstOccurrence = firstOcc;
        }

        public int compareTo(IndexEntry<T> other) {         
            if(this.occurrenceCount < other.getOccurrenceCount()){
                return 1;
            }
            else if(this.occurrenceCount > other.getOccurrenceCount()){
                return -1;
            }
            else{//Need to compare Initial Positions
                if(this.firstOccurrence == other.getFirstOccurrence()){
                    return this.getLocationName().compareTo(other.getLocationName());
                }
                else if(this.firstOccurrence > other.getFirstOccurrence()){
                    return 1;
                }
                else{
                    return -1;
                }
            }
        }   

        public String getLocationName(){
            if(theLocation instanceof File){
                return ((File) theLocation).getAbsolutePath();
            }
            else{
                return theLocation.toString();
            }
        }

        public Integer getFirstOccurrence(){
            return firstOccurrence;
        }

        public Integer getOccurrenceCount(){
            return this.occurrenceCount;
        }

        public void addToOccurrenceCount(Integer toAdd){
            occurrenceCount += toAdd;
        }

        public void setFirstOccurrence(Integer firstOcc){
            firstOccurrence = new Integer(firstOcc);
        }
    }

1 个答案:

答案 0 :(得分:2)

您的代码在显示时会生成您提供的输出,而不是您声明的输出。

以下是一个独立的演示:

https://gist.github.com/4056538