Java 相当于 C# 排序列表
可能的重复:
java中的排序集合
我想知道Java是否有自己的排序列表版本,或者如果我需要创建我自己的。我希望列表在删除某些内容时自动更新。例如,如果我从列表的开头甚至中间删除某些内容,我希望它后面的所有内容在列表中向上移动,并删除剩余的空值空间。
Possible Duplicate:
sorted collection in java
I was wondering if Java has its own version of Sorted List, or if I need to create my own. I want the list to automatically update itself if something is removed. For example, if I remove something from the start of the list, or even in the middle, I want everything behind it to move up in the list, and the remaining null value space to be removed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您确实想要 .NET
SortedList
的等效项(它实际上是按其键排序的映射),那么最接近的等效项可能是TreeMap
。实际上,这更像是SortedDictionary
而不是SortedList
,因为它是一棵树而不仅仅是一个列表,但它可能是最接近的可用选项。但是,您所描述的更像是
ArrayList
,它类似于 .NET 的List
。If you're really after the equivalent of a .NET
SortedList
, which is actually a map ordered by its keys, then the closest equivalent is probablyTreeMap
. That's actually more likeSortedDictionary
thanSortedList
, given that it's a tree rather than just a list, but it's probably the closest available option.However, what you've described is more like
ArrayList
, which is similar to .NET'sList<T>
.好吧,Java 有很多比数组更智能的列表实现,尽管听起来您并不真正想要从描述中得到一个排序列表。
ArrayList 或 LinkedList 将执行您想要的插入或删除元素操作:
您真的想要一个排序列表,还是只是比数组更高级别的东西?
Well, Java has quite a few list implementations that are smarter than arrays, though it doens't sound like you really want a sorted list from your description.
An ArrayList or LinkedList will do what you want as far as inserting or removing elements:
Do you really want a sorted list, or just something higher-level than an array?
java.util.PriorityQueue
< /a>这基本上是一个堆,允许按顺序从前面读取,并允许删除通过
Iterator.remove
从中间开始,但迭代器不按任何特定顺序进行迭代。如果您想要一些可以按顺序迭代的东西,并且不需要重复,那么
TreeSet
是您最好的选择。如果你需要欺骗,那么看看 Apache common 的库TreeBag
。java.util.PriorityQueue
This is basically a heap that allows reading from the front in order, and allows removal from the middle via
Iterator.remove
but the iterator does not iterate in any particular order.If you want something that you can iterate over in order, and don't need dupes, then a
TreeSet
is your best bet. If you need dupes, then look at a library like Apache common'sTreeBag
.