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);
			}
			
			@Override
			protected boolean lessThan(Integer a, Integer b) {
							
                           return (myComparator.compare(o1, o2) > 0);
			}
	};
  
 

 

 

 

 

你可能感兴趣的:(Lucene)