Which is better to use among ArrayList and LinkedHashMap for better speed if key/value datastructure is required

StackOverflow https://stackoverflow.com/questions/21815035

  •  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?

有帮助吗?

解决方案

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).

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top