简体   繁体   English

指向Java LinkedList节点的指针

[英]Pointer into Java LinkedList Node

I am pushing n entries into Java LinkedList at O(1). 我在O(1)处将n个条目推送到Java LinkedList
There are few unique items i would like to remove later on at O(1). 我希望稍后在O(1)删除几个独特的项目。
I though about keeping an array with "pointers" to the unique nodes at the LinkedList so i can later on remove them. 我想保留一个带有“指针”的数组到LinkedList的唯一节点,以便我以后可以删除它们。
is there a way to do it on LinkedList or any other java class? 有没有办法在LinkedList或任何其他Java类上做到这一点?
I tried storing iterators to the items. 我尝试将迭代器存储到项目中。 So i can use iter.remove() . 所以我可以使用iter.remove() But i understood that there can be only one iterator on a list at the time. 但我明白当时列表中只能有一个迭代器。

I know that an easy solution could be implementing link list on my own. 我知道一个简单的解决方案可能是我自己实现链接列表。 But i rather use LinkedList or some other Java class already implemented. 但我宁愿使用LinkedList或其他已经实现的Java类。

The Java List implementations do not offer O(1) performance on removes*. Java List实现在remove *上不提供O(1)性能。 Using an iterator you have to traverse to the index ( O(n) ), with an ArrayList you have an after-remove shift ( O(n) ), and even though LinkedList is a doubly-linked list, it does not expose the nodes in the list for you to be able to remove them directly by simply reassigning the next/last references - a traversal is required to find the node to remove ( O(n) ). 使用迭代器,你必须遍历索引(O(n)),使用ArrayList你有一个删除后的移位(O(n)),即使LinkedList是一个双向链表,它也不会暴露通过简单地重新分配下一个/最后一个引用,列表中的节点可以直接删除它们 - 需要遍历才能找到要删除的节点(O(n))。

You could write your own MyDoublyLinkedList<MyNode<T>> that did so, exposing the nodes rather than the values contained within, which would allow for O(1) removals if you retained a reference to the node. 您可以编写自己的MyDoublyLinkedList<MyNode<T>> ,这样做会暴露节点而不是其中包含的值,如果保留对节点的引用,则允许O(1)删除。 Indexing is of course O(n) to get something from the list by index. 索引当然是O(n)从索引列表中获取内容。 Depending on your use patterns it may be a viable design choice. 根据您的使用模式,它可能是一个可行的设计选择。

Short of that, if you want to use the data structures provided by Java, use a data structure that provides that performance: 如果您想使用Java提供的数据结构,请使用提供该性能的数据结构:

If ordering isn't important and there are no duplicate items, use a HashSet (note, however, this does not allow direct indexing at all) 如果排序不重要且没有重复项,请使用HashSet (但请注意,这根本不允许直接索引)

Otherwise, reworking your code to use a Map implementation is probably your best bet. 否则,重新编写代码以使用Map实现可能是您最好的选择。

[*] LinkedList and the Deque implementations are O(1) for head/tail removal. [*] LinkedListDeque实现是O(1)用于头/尾移除。

Edit to add from comments: 编辑以添加评论:

As mentioned above, no, there is no O(1) time complexity remove operation. 如上所述,不,没有O(1)时间复杂度删除操作。

The reason for this is that except in the most extreme edge cases, it's irrelevant. 这样做的原因是,除了在最极端的情况下,它是无关紧要的。

On my 5 year old, 3Ghz desktop it takes .2ms (point-two) for the worst-case O(n) removal on a LinkedList with 100,000 entries via an index (that is to say, index = length/2) 在我5岁的3Ghz桌面上,对于LinkedList上最坏情况的O(n)删除需要.2ms(第二点),通过索引有100,000个条目(也就是说,index = length / 2)

I start running into windows swapiness disk I/O due to windows memory management on this box if I up it to 1,000,000 entries. 我开始运行Windows交换磁盘I / O,因为这个盒子上的Windows内存管理,如果我把它增加到1,000,000个条目。

In short, you're trying to solve a problem that doesn't exist in most cases. 简而言之,您正在尝试解决大多数情况下不存在的问题。 This is generally called "Premature Optimization" 这通常被称为“过早优化”

The problem with storing a pointer to the item you wish to remove is that removing an item from a linked list requires knowledge of the item preceding the item you wish to remove as well. 存储指向要删除的项目的指针的问题是从链接列表中删除项目需要知道您要删除的项目之前的项目。

A LinkedList is not designed for removing specific items at low cost - you should re-evaluate what List you use (an ArrayList removes in O(1)). LinkedList不是为低成本删除特定项而设计的 - 您应该重新评估您使用的List(在O(1)中删除ArrayList)。 Is there any reason you want a LinkedList? 你有什么理由想要一个LinkedList吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM