Java.util 包中是否有可索引的排序列表?

发布于 2024-10-04 00:22:17 字数 198 浏览 4 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

枫林﹌晚霞¤ 2024-10-11 00:22:17

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 an ArrayList, but it doesn't matter), and I do all the insertions using Collections.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 method insertSorted that would use Collections.binarySearch to do the insertion.

囚你心 2024-10-11 00:22:17

不存在满足您所有标准的简单数据结构。

据我所知,唯一能满足所有这些要求的就是可索引的跳过列表。然而,我不知道有任何现成的 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.

不一样的天空 2024-10-11 00:22:17

非常相似

,看看< a href="https://stackoverflow.com/questions/4031572/sorted-array-list-in-java/4031849#4031849">我对该问题的回答。

基本上它建议如下:

class SortedArrayList<T> extends ArrayList<T> {

    @SuppressWarnings("unchecked")
    public void insertSorted(T value) {
        add(value);
        Comparable<T> cmp = (Comparable<T>) value;
        for (int i = size()-1; i > 0 && cmp.compareTo(get(i-1)) < 0; i--) {
            T tmp = get(i);
            set(i, get(i-1));
            set(i-1, tmp);
        }
    }
}

关于您的第一个要求的注释:“元素的数量是无限的。”:

您可能希望将其限制为“元素的数量不应受到更少的限制”大于 231-1...”,否则您将排除 Java 数组支持的所有选项。 (您可以使用 LinkedList 等任意数量的元素,但我不知道如何在其中进行快速查找。)

This question is very similar to

Have a look at my answer to that question.

Basically it suggests the following:

class SortedArrayList<T> extends ArrayList<T> {

    @SuppressWarnings("unchecked")
    public void insertSorted(T value) {
        add(value);
        Comparable<T> cmp = (Comparable<T>) value;
        for (int i = size()-1; i > 0 && cmp.compareTo(get(i-1)) < 0; i--) {
            T tmp = get(i);
            set(i, get(i-1));
            set(i-1, tmp);
        }
    }
}

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.)

送君千里 2024-10-11 00:22:17

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.

岁月染过的梦 2024-10-11 00:22:17

考虑将 ListCollections.sort() 结合使用。

Consider List combined with Collections.sort().

寄风 2024-10-11 00:22:17

按照 dwb 使用 ListCollections.sort() 的说明,您可以使用 ArrayList 来实现 List(并且不会像 Vector 那样同步,除非您当然想要这种开销)。这可能是你最好的选择,因为他们(Sun)通常对这些领域进行大量研究(无论如何,从我所看到的来看)。如果您需要按“默认”以外的其他方式排序(即您不对整数列表等进行排序),请提供您自己的比较器。

编辑:唯一不满足您的要求的是快速删除......

Going with what dwb stated with List<T> and Collections.sort(), you can use ArrayList<T> as that implements List<T> (and is not synchronized like Vector<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...

临风闻羌笛 2024-10-11 00:22:17

查看 PriorityQueue

如果您的数据结构中不需要类似的元素,那么通常的 TreeSet 也适合您的要求。

Look at PriorityQueue.

If you don't need similar elements in your data structure, then usual TreeSet also fits your requirements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文