Java – treemap deletes all keys larger than a key
•
Java
In a project, I need to delete all objects whose key value is greater than a certain key (the key type is date, if important)
As far as I know, the treemap implemented in Java is a red black tree, which is a binary search tree Therefore, when deleting the subtree, I should get o (n) But I can't find any way to do this except to make the tail view and delete it one by one, which requires o (logn)
Is there a good idea to implement this function? I believe treemap is the correct data structure and should be able to do this
Thank you in advance
Solution
It's simple. Instead of deleting items one by one, use map Clear() deletes the element In the code:
map.tailMap(key).clear();
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
二维码