HashMap vs ArrayList 性能我是正确的 [英] HashMap vs ArrayList performance am I correct

查看:28
本文介绍了HashMap vs ArrayList 性能我是正确的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前认为:

  • 当您需要一个从中随机检索项目的结构时 - 使用 HashMap
  • 当您将按顺序检索项目时(例如使用 for 循环) - 使用 ArrayList

我通常是正确的吗?是否存在不正确的情况?

Am I generally correct? Are there situations where this is not correct?

推荐答案

一般来说,是的,你是对的.还有一个组合数据结构,LinkedHashMap,它提供对任意元素的快速访问以及可预测的排序.

Generally, yes, you are correct. There's also a combined data structure, the LinkedHashMap, which offers fast access to arbitrary elements as well as predictable ordering.

但是,值得注意的是,ArrayList 和 HashMap 分别只是 List 和 Map 接口的两种实现.每个都有其他实现可能更适合更具体的要求.例如,对于某些排队/出队要求,LinkedList 可能比 ArrayList 提供更高的性能.

However, it's worth noting that ArrayList and HashMap are only two implementations of the List and Map interfaces, respectively. There are other implementations of each that might be more suitable for more specific requirements. For example, a LinkedList might provide higher performance than an ArrayList for certain queueing/dequeueing requirements.

这篇关于HashMap vs ArrayList 性能我是正确的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆