Java自带 PriorityQueue实现类lucene段合并,当需要自定义比较器的时候没有Lucene自实现的PriorityQueue好用.
1.封装类型 Book
- package com.taobao.terminator.allen.LuceneTest;
- public class Book
- {
- private String name;
- private int id;
- private String owner;
-
- public Book (String name, String owner, int id) {
- this.name = name;
- this.owner = owner;
- this.id = id;
- }
- public String getOwner() {
- return owner;
- }
- public void setOwner(String owner) {
- this.owner = owner;
- }
-
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- }
2.比较器
- import java.util.Comparator;
- import java.util.List;
- public class AllenComparator implements Comparator<List<Book>>{
-
-
-
- public int compare(List<Book> o1, List<Book> o2) {
- return o1.get(0).getName().compareTo(o2.get(0).getName());
- }
-
- }
3.PriorityQueue代码测试
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
- import java.util.PriorityQueue;
- public class PriorityQueueTest {
- private List<Book> list1 ;
- private List<Book> list2 ;
- private List<Book> list3 ;
-
- public PriorityQueueTest (){
-
- this.list1 = new ArrayList<Book>();
- this.list2 = new ArrayList<Book>();
- this.list3 = new ArrayList<Book>();
-
- initData(list1, list2, list3);
- }
-
- private void initData(List<Book> list1, List<Book> list2, List<Book> list3) {
- generatorData(list1, "owner-1", 1);
- generatorData(list2, "owner-2", 2);
- generatorData(list3, "owner-3", 3);
- }
-
-
-
-
-
- private void generatorData(List<Book> list, String owner, int i) {
- int k = 0;
- while(k < i) {
- list.add(new Book("name-ini" + i, owner, k));
- k++;
- }
- }
-
-
- public static void main(String[] args) {
-
- PriorityQueueTest test = new PriorityQueueTest();
-
- Comparator<List<Book>> comparator = new AllenComparator();
- PriorityQueue<List<Book>> queue = new PriorityQueue<List<Book>>(3, comparator);
- PriorityQueue<List<Book>> matchQueue = new PriorityQueue<List<Book>>(3, comparator);
-
- queue.add(test.list1);
- queue.add(test.list2);
- queue.add(test.list3);
-
- while(!queue.isEmpty()) {
- List<Book> list = queue.poll();
- matchQueue.add(list);
- System.out.println("name--->" + list.get(0).getName() + " owner--->" + list.get(0).getOwner() + " id--->" + list.get(0).getId());
- List<Book> cList = queue.peek();
- while (cList != null && list.get(0).getName().equals(cList.get(0).getName())) {
- List<Book> nlist = queue.poll();
- matchQueue.add(nlist);
- System.out.println("name--->" + nlist.get(0).getName() + " owner--->" + nlist.get(0).getOwner() + " id--->" + nlist.get(0).getId());
- cList = queue.peek();
- }
-
- while(matchQueue.size() > 0) {
- List<Book> mList = matchQueue.poll();
- mList.remove(0);
- if(mList.size() > 0) {
- queue.add(mList);
- }
- }
- }
- }
- }
4.结果输出
- name--->name-ini1 owner--->owner-1 id--->0
- name--->name-ini2 owner--->owner-2 id--->0
- name--->name-ini2 owner--->owner-2 id--->1
- name--->name-ini3 owner--->owner-3 id--->0
- name--->name-ini3 owner--->owner-3 id--->1
- name--->name-ini3 owner--->owner-3 id--->2