Recently, in version R2022b they announced the introduction of dictionaries.
I was under the impression that dictionaries were already available, provided by containers.Map. Are dictionaries just a different name mapped to containers.Map? Or are there other differences? I was unable to find anything comparing them online.
CodePudding user response:
From what I can gather, after reading this blog post and the comments under it, and the documentation (I haven’t yet had a chance to experiment with them, so feel free to correct me if I’m wrong):
dictionary
is an actual primitive type, likedouble
,cell
orstruct
.containers.Map
is a “custom class”, even if nowadays the code is built-in, the functionality can never be as integrated as for a primitive type. Consequently,dictionary
is significantly faster.dictionary
uses normal value semantics. If you make a copy you have two independent dictionaries (note MATLAB’s lazy copy mechanism).containers.Map
is a handle class, meaning that all copies point to the same data, modifying one copy modifies them all.containers.Map
can usechar
arrays (the old string format) or numbers as keys (string
is implicitly converted tochar
when used as key).dictionary
can use any type, as long as it overloadskeyhash
. This means you can use your own custom class objects as keys.dictionary
is vectorized, you can look up multiple values at once. With acontainers.Map
you can look up multiple values using thevalues
function, not the normal lookup syntax.dictionary
has actual O(1) lookup.If I remember correctly,*containers.Map
doesn’t.
* No, it is also O(1), at least in R2022b.