合并排序代码实现
results.add(messages + "各种东西加进去");
List<Message> mergeMsgs = new ArrayList<>();
final Queue<Message> priorityQueue = new PriorityQueue<Message>(results.size(), new MessageComparator());
final Map<String, Iterator<Message>> index = new HashMap<>();
for (List<Message> list : results) {
Iterator<Message> iterator = list.iterator();
Message firstMsg = iterator.next();
index.put(firstMsg.getTargetId(), iterator);
priorityQueue.add(firstMsg);
}
while (mergeMsgs.size() < count && !priorityQueue.isEmpty()) {
Message tmp = priorityQueue.poll();
mUserIdsToMsgIds.put(tmp.getTargetId(), tmp.getMessageId());
if (tmp.getMessageDirection() != Message.MessageDirection.SEND ||
AcccountIdentifier.isOriginUserId(tmp.getTargetId())) {
mergeMsgs.add(tmp);
}
Iterator<Message> iterator = index.get(tmp.getTargetId());
if (iterator.hasNext()) {
priorityQueue.add(iterator.next());
}
}