As far as performance is considered go for LinkedHashMap
as get(), put(), remove(). conyainsKey()
all are O(1) operations and your insertion order is retained. If you use ArrayList
it will be O(N).
Which is better to use among ArrayList and LinkedHashMap for better speed if key/value datastructure is required
-
12-10-2022 - |
题
I need to maintain key/value pairs. I need to maintain the insertion order also. I know two approaches to do it:
Using ArrayList where each object would be of type entity containing key/value pairs as in ListData described below:
class ListData{
String key;
ArrayList<MyData> dataList;
}
Using LinkedHashMap for maintaing the key value pairs.
Is there any performance/speed problem in terms of speed in using LinkedHashMap?
Can someone suggest the best approach to do this?
解决方案
其他提示
No doubt Linked Hash Map is better in performance with comparison with ArrayList.I have doubt how you going to maintain Key/value pair in list.
But if you want to maintain key/value pair with ordering then go with Linked Hash Map
otherwise Hash Map
.
不隶属于 StackOverflow