Java PriorityQueue实现类lucene段合并


Java自带 PriorityQueue实现类lucene段合并,当需要自定义比较器的时候没有Lucene自实现的PriorityQueue好用.

1.封装类型 Book

  1. package com.taobao.terminator.allen.LuceneTest;   
  2. public class Book    
  3. {   
  4.     private String name;   
  5.     private int id;   
  6.     private String owner;   
  7.        
  8.     public Book (String name, String owner, int id) {   
  9.         this.name = name;   
  10.         this.owner = owner;   
  11.         this.id = id;   
  12.     }   
  13.     public String getOwner() {   
  14.         return owner;   
  15.     }   
  16.     public void setOwner(String owner) {   
  17.         this.owner = owner;   
  18.     }   
  19.        
  20.     public String getName() {   
  21.         return name;   
  22.     }   
  23.     public void setName(String name) {   
  24.         this.name = name;   
  25.     }   
  26.     public int getId() {   
  27.         return id;   
  28.     }   
  29.     public void setId(int id) {   
  30.         this.id = id;   
  31.     }   
  32. }  

2.比较器

  1. import java.util.Comparator;   
  2. import java.util.List;   
  3. public class AllenComparator implements Comparator<List<Book>>{   
  4.     /**  
  5.      * 比较二个List的大小,以第一个元素的姓名值作比较  
  6.      */  
  7.     public int compare(List<Book> o1, List<Book> o2) {   
  8.         return o1.get(0).getName().compareTo(o2.get(0).getName());   
  9.     }   
  10.        
  11. }  

3.PriorityQueue代码测试

  1. import java.util.ArrayList;   
  2. import java.util.Comparator;   
  3. import java.util.List;   
  4. import java.util.PriorityQueue;   
  5. public class PriorityQueueTest {   
  6.     private List<Book> list1 ;   
  7.     private List<Book> list2 ;   
  8.     private List<Book> list3 ;   
  9.        
  10.     public PriorityQueueTest (){   
  11.            
  12.         this.list1 = new ArrayList<Book>();   
  13.         this.list2 = new ArrayList<Book>();   
  14.         this.list3 = new ArrayList<Book>();   
  15.            
  16.         initData(list1, list2, list3);   
  17.     }   
  18.        
  19.     private void initData(List<Book> list1, List<Book> list2, List<Book> list3) {   
  20.         generatorData(list1, "owner-1"1);   
  21.         generatorData(list2, "owner-2"2);   
  22.         generatorData(list3, "owner-3"3);   
  23.     }   
  24.     /**  
  25.      * 生成List数据集  
  26.      * @param list  
  27.      * @param i 相同数据重复次数  
  28.      */  
  29.     private  void generatorData(List<Book> list, String owner, int i) {   
  30.         int k = 0;   
  31.         while(k < i) {   
  32.             list.add(new Book("name-ini" + i, owner, k));   
  33.             k++;   
  34.         }   
  35.     }   
  36.        
  37.        
  38.     public static void main(String[] args) {   
  39.            
  40.         PriorityQueueTest test = new PriorityQueueTest();   
  41.            
  42.         Comparator<List<Book>> comparator = new AllenComparator();   
  43.         PriorityQueue<List<Book>> queue = new PriorityQueue<List<Book>>(3, comparator);    
  44.         PriorityQueue<List<Book>> matchQueue = new PriorityQueue<List<Book>>(3, comparator);    
  45.            
  46.         queue.add(test.list1);   
  47.         queue.add(test.list2);   
  48.         queue.add(test.list3);   
  49.            
  50.         while(!queue.isEmpty()) {   
  51.             List<Book> list = queue.poll();   
  52.             matchQueue.add(list);   
  53.             System.out.println("name--->" + list.get(0).getName() + " owner--->" + list.get(0).getOwner() + " id--->" + list.get(0).getId());   
  54.             List<Book> cList = queue.peek();   
  55.             while (cList != null && list.get(0).getName().equals(cList.get(0).getName())) {   
  56.                 List<Book> nlist = queue.poll();   
  57.                 matchQueue.add(nlist);   
  58.                 System.out.println("name--->" + nlist.get(0).getName() + " owner--->" + nlist.get(0).getOwner() + " id--->" + nlist.get(0).getId());   
  59.                 cList = queue.peek();   
  60.             }   
  61.             //matchingQueue   
  62.             while(matchQueue.size() > 0) {   
  63.                 List<Book> mList = matchQueue.poll();   
  64.                 mList.remove(0);   
  65.                 if(mList.size() > 0) {   
  66.                     queue.add(mList);   
  67.                 }   
  68.             }   
  69.         }   
  70.     }   
  71. }  

4.结果输出

  1. name--->name-ini1 owner--->owner-1 id--->0  
  2. name--->name-ini2 owner--->owner-2 id--->0  
  3. name--->name-ini2 owner--->owner-2 id--->1  
  4. name--->name-ini3 owner--->owner-3 id--->0  
  5. name--->name-ini3 owner--->owner-3 id--->1  
  6. name--->name-ini3 owner--->owner-3 id--->2  

相关内容