读书人

lucene中PriorityQueue种的功能

发布时间: 2012-08-15 16:57:16 作者: rapoo

lucene中PriorityQueue类的功能

org.apache.lucene.util.PriorityQueue

?

PriorityQueue<T>类实际上是一个最大堆,在N中找出最小的M个数字,可以用这个类来做。

?

可以自己写一个Comparator<T>,那么可以控制为最大堆,还是最小堆。

?

        Comparator<Integer> myComparator= new Comparator<Integer>() {            @Override            public int compare(Integer a, Integer b) {                return a.compareTo(b);//如果return -a.compareTo(b);就是最大堆            }        };             PriorityQueue<Integer> pq = new PriorityQueue<Integer>() {{this.initialize(maxSize);}@Overrideprotected boolean lessThan(Integer a, Integer b) {                           return (myComparator.compare(o1, o2) > 0);}};  
?

?

?

?

?

读书人网 >互联网

热点推荐