[英]Hashmap vs Bundle in Android Efficiency and Performance Comparison
I am trying to understand the performance impact of using a HashMap
vs a Bundle
in an Android program.我试图了解在 Android 程序中使用
HashMap
与Bundle
对性能的影响。 I understand that a Bundle
is a specialized Android component - does it therefore make it superior to a HashMap
if one needed to simply store basic data types ( Integer
s, String
s etc.) and not complex objects?我知道
Bundle
是一个专门的 Android 组件 - 因此如果需要简单地存储基本数据类型( Integer
s,而不是String
),它是否优于HashMap
?
Bundle
s are actually built on top of ArrayMap
s, which are memory-efficient implementations of Map
s. Bundle
实际上是建立在 ArrayMap 之上的, ArrayMap
是Map
的内存高效实现。 For smaller ArrayMap
s, a search takes less computational time than computing the hash for a similarly populated HashMap
.对于较小的
ArrayMap
,搜索所需的计算时间比为类似填充的HashMap
计算 hash 所需的计算时间更少。 But if you have more than a few hundred items in it, the HashMap
will perform better.但是如果你有超过几百个项目,
HashMap
会表现更好。
Besides, Bundle
s were not designed to be general-purpose data structures.此外,
Bundle
并非设计为通用数据结构。
Bundle allows to put String, integer, boolean, etc. into it but a HashMap allows only string, for others you have to convert them to/from objects. Bundle允许将 String、integer、boolean 等放入其中,但 HashMap 仅允许字符串,对于其他人,您必须将它们转换为对象/从对象转换。
One more difference is that with a Bundle, every object it contain is parcelable.另一个区别是,对于一个 Bundle,它包含的每个 object 都是可包裹的。 This allows Bundles to be used as service parameters and to be attached to intents or fragments to passed into activities or fragments.
这允许 Bundles 用作服务参数并附加到意图或片段以传递到活动或片段中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.