java有跳过列表实现吗
I find ConcurrentSkipListSet
in Java Collection Framework, which is backed up with a skip list. But is there a skip list in Java? A set does not work in my use case. I need a indexable list that supports duplicates.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这个答案晚了 3 年,但我希望它对那些从现在开始想要 Java 跳过列表的人有用:)
这个解决方案允许按照您的要求进行重复。我大致遵循这里的指南 http://igoro.com/archive/skip-lists-are-fascinating,所以复杂性与此类似,除了 删除 成本为 O(nlogn) - 因为我不费心使用双向链接节点,我想这样做会带来 删除 下降到O(logn)。
代码包括:接口、实现该接口的跳表、节点类。它也是通用的。
您可以调整参数LEVELS以提高性能,但请记住时空权衡。
This answer is 3 years late but I hope it will be useful for those wanting a Java skip list from this moment on :)
This solution allows duplicates as you asked. I follow roughly the guide here http://igoro.com/archive/skip-lists-are-fascinating, so the complexities are similar to that, except delete costs O(nlogn) - as I didn't bother using doubly-linked nodes, I imagine doing so would bring delete down to O(logn).
Code comprises of: an interface, the skip list implementing the interface, and the node class. It is also generic.
You can tune the parameter LEVELS for performance, but remember the space-time tradeoff.
修复了@PoweredByRice 提供的实现中的错误。当删除的节点是第一个节点时,它会抛出 NPE。其他更新包括重命名变量名称和反向打印跳过列表的顺序。
Fixed the bug in the implementation provided by @PoweredByRice. It threw an NPE for cases when the node deleted was the first node. Other updates include renamed variable names and reverse printing the order of the skip list.
由于您提到了一个既可索引(我假设您想要快速检索)又需要允许重复的列表,我建议您使用带有 LinkedList 或 ArrayList 的自定义集。
您需要有一个基本 Set(例如 HashSet),并不断向其中添加值。如果您遇到重复项,则该 Set 的值应指向 List。因此,您将拥有快速检索,当然您将以伪集合的方式存储您的对象。
这将为您提供良好的检索效率。理想情况下,如果您的密钥不重复,您将实现 O(1) 作为检索速度。
Since you've mentioned a List that is both Indexable (I assume you want speedy retrieval) and need to allow duplicates, I would advise you go for a custom Set with a LinkedList or ArrayList perhaps.
You need to have a base Set, an HashSet for example and keep adding values to it. If you face a duplicate, the value of that Set should point to a List. So, that you will have both Speedy retrieval and of course you will store your objects in a psuedo Collection manner.
This should give you good efficiency for retrieval. Ideally if your Keys are not duplicates, you will achieve an O(1) as the retrieval speed.
创建
ConcurrentSkipListSet
时,您将比较器传递给构造函数。您可以创建一个比较器,使您的
SkipListSet
表现得像普通列表一样。When you create a
ConcurrentSkipListSet
, you pass a comparator to the constructor.You could create a comparator that will make your
SkipListSet
behave as a normal List.您可以使用以下代码来编写您自己的基本跳过列表:
1)使开始和结束
代表开始和结束跳过列表
。2)
添加节点
并分配指针
到下一个基于
Java代码的基本跳过列表(您可以添加如果需要更多功能):
输出:
--简单遍历--
1=>2=>3=>4=>5=>6=>
--快车道穿越--
2==>4==>6==>
You can make use the below to code make your own basic skiplist :
1)Make start and end to
represent start and end of skip list
.2)
Add the nodes
andassign pointers
to nextbased on
Java code for basic skip list (you can add more features if you want):
Output:
--Simple Traversal--
1=>2=>3=>4=>5=>6=>
--Fast Lane Traversal--
2==>4==>6==>
我同意使用 apache-collections 中的 TreeList 并使用 Happy Java Libraries 中的 SortedList 对其进行装饰
https://sourceforge.net/p/happy-guys/wiki/Sorted% 20列表/
I approve to use TreeList from apache-collections and decorate it with SortedList from Happy Java Libraries
https://sourceforge.net/p/happy-guys/wiki/Sorted%20List/
我并不是说这是我自己的实现。我只是不记得在哪里找到它的。如果您知道请告诉我,我会更新。这对我来说效果很好:
I am not claiming that this is my own implementation. I just cannot remember where I found it. If you know let me know and I will update. This has been working quite well for me: