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

查看:98
本文介绍了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.

推荐答案

A 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天全站免登陆