一个很好的Java排序列表 [英] A good Sorted List for Java

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

问题描述

我正在为java寻找一个好的排序列表。谷歌搜索给我一些关于使用TreeSet / TreeMap的提示。但是这些组件缺少一件事:随机访问集合中的元素。
例如,我想访问有序集合中的第n个元素,但是使用TreeSet,我必须遍历其他n-1个元素才能到达那里。这将是一种浪费,因为我的Set中有多达数千个元素。

I'm looking for a good sorted list for java. Googling around give me some hints about using TreeSet/TreeMap. But these components is lack of one thing: random access to an element in the set. For example, I want to access nth element in the sorted set, but with TreeSet, I must iterate over other n-1 elements before I can get there. It would be a waste since I would have upto several thousands elements in my Set.

基本上,我正在寻找类似于.NET中的排序列表的东西,能够快速添加元素,快速删除元素,并随机访问列表中的任何元素。

Basically, I'm looking for some thing similar to a sorted list in .NET, with ability to add element fast, remove element fast, and have random access to any element in the list.

这种排序列表是否在某处实现?
谢谢。

Has this kind of sorted list implemented somewhere? Thanks.

已编辑

我对SortedList的兴趣不断增长这个问题:
我需要维护一个包含数千个对象的列表(并且可以增长到数十万个)。这些对象将持久保存到数据库中。我想从整个列表中随机选择几十个元素。因此,我尝试维护一个单独的内存列表,其中包含所有对象的主键(长号)。当从数据库添加/删除对象时,我需要从列表中添加/删除键。我现在正在使用ArrayList,但是当记录数量增长时,我担心ArrayList不适合它。 (想象一下,每次从数据库中删除对象时,都必须迭代数十万个元素)。回到我进行.NET编程的时候,我会使用一个排序的List(List是一个.NET类,一旦Sorted属性设置为true,将维护其元素的顺序,并提供帮助删除/插入元素的二进制搜索很快)。我希望我能从java BCL找到类似的东西,但不幸的是,我没有找到一个好的匹配。

My interest in SortedList grows out of this problems: I need to maintains a list of many thousands object (and can grow up to many hundred of thousands). These objects will be persisted to database. I want to randomly select few dozens of element from the whole list. So, I tried to maintain a separated on-memory list that contains the primary keys (Long numbers) of all objects. I need to add/remove keys from the list when object is added / removed from database. I'm using an ArrayList right now but I'm afraid ArrayList would not suit it when the number of records grows. (Imagine you have to iterate over several hundred thousands of elements every time an object is removed from database). Back to the time when I did .NET programming, then I would use a sorted List (List is a .NET class that once Sorted property set to true, will maintain order of its element, and provide binary search that help remove/insert element very quick). I'm hoping that I can find some thing similar from java BCL but unluckily, I didn't find a good match.

推荐答案

您似乎希望列表结构具有非常快速的删除和随机访问按索引(而非按键)次。 ArrayList 为您提供后者和 HashMap TreeMap 给你前者。

It seems that you want a list structure with very fast removal and random access by index (not by key) times. An ArrayList gives you the latter and a HashMap or TreeMap give you the former.

Apache Commons Collections中有一个结构可能正是你要找的, TreeList 。 JavaDoc指定它已针对列表中的任何索引进行快速插入和删除进行了优化。如果您还需要泛型,这对您没有帮助。

There is one structure in Apache Commons Collections that may be what you are looking for, the TreeList. The JavaDoc specifies that it is optimized for quick insertion and removal at any index in the list. If you also need generics though, this will not help you.

这篇关于一个很好的Java排序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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