why the default implemention of CookieStore do not use a tree-like struct?
-
27-06-2021 - |
Question
InMemoryCookieStore is the default CookieStore of java , but the main store is a ArrayList, the add and remove op will cost O(n) in worst case ,and index is map , which scan the keySet in order to remove expired cookie. why it use tree(which can represent domain very well,like .b.com is the parent of .a.b.com ) and set (locate Cookie in constant time) .
Solution
The CookieStore has a method
List<HttpCookie> getCookies()
so it must maintain, or copy all the cookies on demand. I assume they decided that maintaining a list was the most efficient.
OTHER TIPS
How often do you have so many cookies that O(n) is noticeably larger than O(1) in terms of performance? When choosing between sub-optimization and code manageability, most go for clean, manageable code.