PriorityQueue和TreeSet在Java中的区别? [英] Difference between PriorityQueue and TreeSet in Java?

查看:2370
本文介绍了PriorityQueue和TreeSet在Java中的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力了解何时使用这两个数据结构。据我所知,PriorityQueue也被实现为一个树,因为文档说明插入删除和包含的平均时间是O(logn)。树木也提供了相同的时间复杂性。此外,它们都是未经解码的实现。而且我可以写比较器让他们像最小堆或最大堆一样行动。

I am trying to understand when to use the two data structures. As far as I have understood the PriorityQueue is also implemented as a tree as the documentation states that the avg time for insert remove and contains is O(logn). The treeset also provides the same time complexity. Plus both of them are unsynchorized implementation. And I can write comparator for them to act like min heap or max heap.

有些人可以指出在什么情况下我使用这两套。

Can some one point out in what conditions I use these two sets.

谢谢,

推荐答案

当你想要一个队列时, PriorityQueue。当你想要一个集合,使用一个树集。 TreeSet具有唯一的元素,并且不提供队列的API。队列不提供集合的API,并允许多个相等的元素。

When you want a queue, use a PriorityQueue. When you want a Set, use a TreeSet. A TreeSet has unique elements, and doesn't offer the API of a Queue. A Queue doesn't offer the API of a Set, and allows multiple equal elements.

这篇关于PriorityQueue和TreeSet在Java中的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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