Java linked list that supports rapid deletion of any node?

java. util. LinkedList does not allow you to quickly delete a given object in the list The remove (object) method performs a linear search to find objects in the list so that they can be deleted Since this is a double linked list, you only need to update the pointers (node.prev and node. Next)

What is the Java standard solution to this problem?

Note 1: I don't want to delete it during iteration I know it's fast, but I'm not iterating in my element first

Note 2: for simplicity: given an object o, I know it is in the double linked list. I want to quickly delete o from the list (by updating the pointer) without linear search in the list, such as Java util. What about LinkedList

Solution

You should take a look at the linkedhashset course Basically, it is a HashSet that maintains a two - way linked list in its entries It supports retrieval (and therefore deletion) of elements in O (1) (I hope so) In the case of reinserting entries and other details, check the link for specifications on how to handle the element order

Edit:

If you need to store duplicates, you can view guava linkedhashmultiset (never used so far) The guava user guide on multiset is here

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>