Java - PriorityQueue 与排序的 LinkedList [英] Java - PriorityQueue vs sorted LinkedList

查看:24
本文介绍了Java - PriorityQueue 与排序的 LinkedList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个实现不那么重":PriorityQueue 或已排序的 LinkedList(使用比较器)?

Which implementation is less "heavy": PriorityQueue or a sorted LinkedList (using a Comparator)?

我想对所有项目进行排序.插入将非常频繁,有时我将不得不运行所有列表以进行一些操作.

I want to have all the items sorted. The insertion will be very frequent and ocasionally I will have to run all the list to make some operations.

推荐答案

LinkedList 是最糟糕的选择.要么使用 ArrayList(或更一般地说,使用 RandomAccess 实现者)或 PriorityQueue.如果确实使用了列表,请仅在迭代其内容之前对其进行排序,而不是在每次插入之后对其进行排序.

A LinkedList is the worst choice. Either use an ArrayList (or, more generally, a RandomAccess implementor), or PriorityQueue. If you do use a list, sort it only before iterating over its contents, not after every insert.

需要注意的一点是PriorityQueue 迭代器没有按顺序提供元素;您实际上必须删除元素(清空队列)以按顺序迭代其元素.

One thing to note is that the PriorityQueue iterator does not provide the elements in order; you'll actually have to remove the elements (empty the queue) to iterate over its elements in order.

这篇关于Java - PriorityQueue 与排序的 LinkedList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆