Updating containers.Map with a new containers.Map - does new key overwrite old?

2 ビュー (過去 30 日間)
Nathan Thern
Nathan Thern 2018 年 8 月 31 日
I refer to the Read and Write Using Key Index documentation.
A new containers.Map can be created by combining two containers.Map:
m1 = containers.Map({1, 5, 8}, {'A', 'B', 'C'});
m2 = containers.Map({8, 9, 6}, {'X', 'Y', 'Z'});
m = [m1; m2];
keys(m), values(m)
ans =
[1] [5] [6] [8] [9]
ans =
'A' 'B' 'Z' 'X' 'Y'
Note that value 'C' for key 8 was overwritten. In the same way I could update Map m1 with the key/value pairs in m2:
m1 = [m1; m2];
keys(m1), values(m1)
ans =
[1] [5] [6] [8] [9]
ans =
'A' 'B' 'Z' 'X' 'Y'
What I would like to know (the documentation isn't clear on this) is:
Is it guaranteed that the value in m2 will overwrite any duplicate key values from m1.
I could take the question a step further with this example:
m1 = containers.Map({1, 5, 8}, {'A', 'B', 'C'});
m2 = containers.Map({8, 9, 6}, {'X', 'Y', 'Z'});
m3 = containers.Map({2, 8, 3}, {'Q', 'R', 'S'});
m1 = [m1; m2; m3];
keys(m1), values(m1)
ans =
[1] [2] [3] [5] [6] [8] [9]
ans =
'A' 'Q' 'S' 'B' 'Z' 'R' 'Y'
It seems that the last appearing value for the key 8 becomes the new value. But is that guaranteed, and will it always be that way in the future?

回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by