Java.util 包中是否有可索引的排序列表?
我正在 java.util 包中寻找数据结构。我需要它满足以下要求:
- 元素的数量(理论上)是无限的。
- 元素按升序排序。
- 您可以(快速)获得第 n 个元素。
- 您可以删除第 n 个元素(快速)。
我希望找到一个可索引的跳过列表,但我没有。他们有满足我所说的要求的数据结构吗?
I'm looking for a data structure in the java.util package. I need it to meet the following requirements:
- The number of elements is (theoretically) unbounded.
- The elements are sorted in an ascending order.
- You can get the nth element (fast).
- You can remove the nth element (fast).
I expected to find an indexable skip list, but I didn't. Do they have any data structure which meets the requirements I'v stated?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Java标准库中没有这样的容器。
当我需要具有这些属性的数据结构时,我使用
List
实现(通常是ArrayList
,但这并不重要),并且使用 < a href="http://download.oracle.com/javase/6/docs/api/java/util/Collections.html#binarySearch(java.util.List,%20T)" rel="noreferrer">Collections.binarySearch
。如果我必须将排序列表封装为可重用类,我将实现 List 接口,将所有方法委托给“标准”List 实现(它甚至可以作为参数传递给构造函数)。我会通过抛出异常 (
UnsupportedOperationException
) 来实现每个插入方法(add、addAll、set、Iterator 的删除),这样就没有人可以破坏“始终排序”属性。最后,我将提供一个insertSorted
方法,该方法将使用Collections.binarySearch
进行插入。There is no such container in the Java standard libraries.
When I need a data structure with these properties, I use a
List
implementation (generally anArrayList
, but it doesn't matter), and I do all the insertions usingCollections.binarySearch
.If I had to encapsulate a sorted list as a reusable class, I'd implement the List interface, delegating all methods to a 'standard' List implementation (it can even be passed as a parameter to the constructor). I'd implement every insertion method (add, addAll, set, Iterator's remove) by throwing an exception (
UnsupportedOperationException
), so that nobody can break the 'always sorted' property. Finally, I'd provide a methodinsertSorted
that would useCollections.binarySearch
to do the insertion.不存在满足您所有标准的简单数据结构。
据我所知,唯一能满足所有这些要求的就是可索引的跳过列表。然而,我不知道有任何现成的 Java 实现。
There exists no simple data structure that fulfills all your criteria.
The only one that I know which does fulfills them all would be an indexable skip list. Hoewever,I don't know of any readily available Java implementations.
非常相似
,看看< a href="https://stackoverflow.com/questions/4031572/sorted-array-list-in-java/4031849#4031849">我对该问题的回答。
基本上它建议如下:
关于您的第一个要求的注释:“元素的数量是无限的。”:
您可能希望将其限制为“元素的数量不应受到更少的限制”大于 231-1...”,否则您将排除 Java 数组支持的所有选项。 (您可以使用 LinkedList 等任意数量的元素,但我不知道如何在其中进行快速查找。)
This question is very similar to
Have a look at my answer to that question.
Basically it suggests the following:
A note on your first requirement: "The number of elements is unbounded.":
You may want to restrict this to something like "The number of elements should not be bound by less than 231-1..." since otherwise you're ruling out all options which are backed by a Java array. (You could get away with an arbitrary number of elements using for instance a LinkedList, but I can't see how you could do fast lookups in that.)
TreeSet
为您提供在向列表添加元素时自然排序的功能。但如果您不需要这个并且允许使用 Collections.sort(),您可以使用简单的 ArrayList。
TreeSet
provides you the functionality of natural sorting while adding elements to the list.But if you don't need this and Collections.sort() is permitted you can use simple
ArrayList
.考虑将
List
与Collections.sort()
结合使用。Consider
List
combined withCollections.sort()
.按照 dwb 使用
List
和Collections.sort()
的说明,您可以使用ArrayList
来实现List
(并且不会像Vector
那样同步,除非您当然想要这种开销)。这可能是你最好的选择,因为他们(Sun)通常对这些领域进行大量研究(无论如何,从我所看到的来看)。如果您需要按“默认”以外的其他方式排序(即您不对整数列表等进行排序),请提供您自己的比较器。编辑:唯一不满足您的要求的是快速删除......
Going with what dwb stated with
List<T>
andCollections.sort()
, you can useArrayList<T>
as that implementsList<T>
(and is not synchronized likeVector<T>
unless of course you want that overhead). That is probably your best bet because they (Sun) typically do lots of research into these areas (from what I've seen anyway). If you need to sort by something other than the "default" (i.e. you are not sorting a list of integers, etc), then supply your own comparator.EDIT: The only thing that does not meet your requirements are the fast removals...
查看 PriorityQueue。
如果您的数据结构中不需要类似的元素,那么通常的 TreeSet 也适合您的要求。
Look at PriorityQueue.
If you don't need similar elements in your data structure, then usual TreeSet also fits your requirements.