Java PriorityQueue实现类lucene段合并
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>>{
- /**
- * 比较二个List的大小,以第一个元素的姓名值作比较
- */
- 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);
- }
- /**
- * 生成List数据集
- * @param list
- * @param i 相同数据重复次数
- */
- 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();
- }
- //matchingQueue
- 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
相关推荐
IT之家 2020-03-11
graseed 2020-10-28
zbkyumlei 2020-10-12
SXIAOYI 2020-09-16
jinhao 2020-09-07
impress 2020-08-26
liuqipao 2020-07-07
淡风wisdon大大 2020-06-06
yoohsummer 2020-06-01
chenjia00 2020-05-29
baike 2020-05-19
扭来不叫牛奶 2020-05-08
hxmilyy 2020-05-11
黎豆子 2020-05-07
xiongweiwei00 2020-04-29
Cypress 2020-04-25
冰蝶 2020-04-20