java - 使用ArrayList<String> 和 使用HashMap<String, String> 存储键值对,哪个速度更快一些
ringa_lee
ringa_lee 2017-04-18 10:41:08
0
6
715
ringa_lee
ringa_lee

ringa_lee

reply all(6)
小葫芦

It is not recommended to use the ArrayList method of storing keys in even numbers and storing values ​​in odd numbers. This goes against the general logic of how we write code and will also cause great trouble to future maintainers. Moreover, the performance of mobile phones nowadays is very good. In your usage scenario, there is no need to consider performance issues.

大家讲道理

For the case where the amount of data you mentioned is less than 10, the error between ArrayList<String> and HashMap<String, String> is very small, and using either one will not affect the performance; but discussing this issue from a technical level, I will just say a few words. ^.^

ArrayList is an ordered collection, and its bottom layer is actually an array. If it is traversed and stored, it is still faster than HashMap, but its addition and deletion will be slower, especially adding and deleting from the middle of the list (distant)

HashMap is an unordered hash table, and its query order is directly related to the amount of data. To put it simply, the larger the amount of data, the slower the query!

Summary:
Small Data: Both can be used.
Big data: ArrayList is frequently used for queries, and HashMap is used for frequent additions, deletions, and changes.
Very large data: use ArrayList;

PHPzhong

This is related to the amount of data

巴扎黑

It depends on how you use it. If you just store and traverse List, it is faster. If you want to check the value by key, Map is faster

PHPzhong

Although I don’t know why you want to compare the storage of List<String> and Map<String, String> which one is faster? There are different implementation classes for these two interfaces, such as ArrayList and LinkedList, which are very different, and HashMap, TreeMap, LinkedHashMap, WeakHashMap, and IdentityHashMap have very different implementation class behaviors, efficiency, object storage cycles, and key equivalence strategies. Just pretend I didn’t answer/(ㄒoㄒ)/~~

小葫芦

The performance has been explained very clearly upstairs.
If the stored data is less than 10, you can directly specify the maximum value of 10 when declaring, which can save memory space.

List<String> list = new ArrayList<>(10);
Map<String, String> map = new HashMap<>(10);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template