Do Maps have an Reliable Relationship between keySet() order and values() order?Is it possible to create a generic Map swapper?Maintaining a reference to sObjects being upserted for subsequent related object insertionApproval Matrix using custom settings and its methodLimit results from listDoes Order Number have to be an autonumber?How to navigate multiple lists/maps and turn into stringWhen should I use Maps and when should I use Lists in Apex?SOQL Query on key value pair?Map keyset order is not same when I iterate on the keysetLooping Collection doesn't show same order they haveIs there any documentation evidence that order of Map keyset converted to List is preserved?
Clarification of algebra in moment generating functions
Why does blending blueberries, milk, banana and vanilla extract cause the mixture to have a yogurty consistency?
Make me a minimum magic sum
Where did Lovecraft write about Carcosa?
Is Iron Man stronger than the Hulk?
Where are the "shires" in the UK?
Motion-trail-like lines
Krull dimension of the ring of global sections
Has the Hulk always been able to talk?
about academic proof-reading, what to do in this situation?
Is there precedent or are there procedures for a US president refusing to concede to an electoral defeat?
All superlinear runtime algorithms are asymptotically equivalent to convex function?
It isn’t that you must stop now
Gerrymandering Puzzle - Rig the Election
How to deal with employer who keeps me at work after working hours
Dangerous workplace travelling
Is there a word for food that's gone 'bad', but is still edible?
How can I decipher which graph belongs to which equation?
How to Practice After Stream Entry as Opposed to Before?
How to pass query parameters in URL in Salesforce Summer 19 Release?
Where to draw the line between quantum mechanics theory and its interpretation(s)?
What Kind of Wooden Beam is this
Madam I m Adam..please don’t get mad..you will no longer be prime
no sense/need/point
Do Maps have an Reliable Relationship between keySet() order and values() order?
Is it possible to create a generic Map swapper?Maintaining a reference to sObjects being upserted for subsequent related object insertionApproval Matrix using custom settings and its methodLimit results from listDoes Order Number have to be an autonumber?How to navigate multiple lists/maps and turn into stringWhen should I use Maps and when should I use Lists in Apex?SOQL Query on key value pair?Map keyset order is not same when I iterate on the keysetLooping Collection doesn't show same order they haveIs there any documentation evidence that order of Map keyset converted to List is preserved?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Let's say I have a map, for example:
Map<SObjectField, SObjectField> valueByKeyMap
Assuming neither the Map nor its contents are mutated by other activity in the system, can I rely on these two lists being identical both in content and order:
1.
List<Object> valueObjectsInKeyOrderList = new List<Object>();
for (SObjectField key : valueByKeyMap.keySet())
valueObjectsInKeyOrderList.add(valueByKeyMap.get(key));
2.
List<Object> valueObjectList = new List<SObjectField>(valueByKeyMap.values());
... such that if I also have:
List<Object> keyObjectList = new List<SObjectField>(valueByKeyMap.keySet());
... and I were to take any arbitrary value out of keyObjectList, i.e.:
SObjectField key = keyObjectList[n];
... all three of these would always result in the same values:
SObjectField result1 = valueByKeyMap.get(key);
SObjectField result2 = valueObjectsInKeyOrderList[n];
SObjectField result3 = valueObjectList [n];
... regardless of the collection type or size?
If so, is this documented behaviour that is unlikely to change?
If not, would there be any way to produce valueObjectList without a loop which results in an identical collection to valueObjectsInKeyOrderList?
apex list map order collection
add a comment |
Let's say I have a map, for example:
Map<SObjectField, SObjectField> valueByKeyMap
Assuming neither the Map nor its contents are mutated by other activity in the system, can I rely on these two lists being identical both in content and order:
1.
List<Object> valueObjectsInKeyOrderList = new List<Object>();
for (SObjectField key : valueByKeyMap.keySet())
valueObjectsInKeyOrderList.add(valueByKeyMap.get(key));
2.
List<Object> valueObjectList = new List<SObjectField>(valueByKeyMap.values());
... such that if I also have:
List<Object> keyObjectList = new List<SObjectField>(valueByKeyMap.keySet());
... and I were to take any arbitrary value out of keyObjectList, i.e.:
SObjectField key = keyObjectList[n];
... all three of these would always result in the same values:
SObjectField result1 = valueByKeyMap.get(key);
SObjectField result2 = valueObjectsInKeyOrderList[n];
SObjectField result3 = valueObjectList [n];
... regardless of the collection type or size?
If so, is this documented behaviour that is unlikely to change?
If not, would there be any way to produce valueObjectList without a loop which results in an identical collection to valueObjectsInKeyOrderList?
apex list map order collection
add a comment |
Let's say I have a map, for example:
Map<SObjectField, SObjectField> valueByKeyMap
Assuming neither the Map nor its contents are mutated by other activity in the system, can I rely on these two lists being identical both in content and order:
1.
List<Object> valueObjectsInKeyOrderList = new List<Object>();
for (SObjectField key : valueByKeyMap.keySet())
valueObjectsInKeyOrderList.add(valueByKeyMap.get(key));
2.
List<Object> valueObjectList = new List<SObjectField>(valueByKeyMap.values());
... such that if I also have:
List<Object> keyObjectList = new List<SObjectField>(valueByKeyMap.keySet());
... and I were to take any arbitrary value out of keyObjectList, i.e.:
SObjectField key = keyObjectList[n];
... all three of these would always result in the same values:
SObjectField result1 = valueByKeyMap.get(key);
SObjectField result2 = valueObjectsInKeyOrderList[n];
SObjectField result3 = valueObjectList [n];
... regardless of the collection type or size?
If so, is this documented behaviour that is unlikely to change?
If not, would there be any way to produce valueObjectList without a loop which results in an identical collection to valueObjectsInKeyOrderList?
apex list map order collection
Let's say I have a map, for example:
Map<SObjectField, SObjectField> valueByKeyMap
Assuming neither the Map nor its contents are mutated by other activity in the system, can I rely on these two lists being identical both in content and order:
1.
List<Object> valueObjectsInKeyOrderList = new List<Object>();
for (SObjectField key : valueByKeyMap.keySet())
valueObjectsInKeyOrderList.add(valueByKeyMap.get(key));
2.
List<Object> valueObjectList = new List<SObjectField>(valueByKeyMap.values());
... such that if I also have:
List<Object> keyObjectList = new List<SObjectField>(valueByKeyMap.keySet());
... and I were to take any arbitrary value out of keyObjectList, i.e.:
SObjectField key = keyObjectList[n];
... all three of these would always result in the same values:
SObjectField result1 = valueByKeyMap.get(key);
SObjectField result2 = valueObjectsInKeyOrderList[n];
SObjectField result3 = valueObjectList [n];
... regardless of the collection type or size?
If so, is this documented behaviour that is unlikely to change?
If not, would there be any way to produce valueObjectList without a loop which results in an identical collection to valueObjectsInKeyOrderList?
apex list map order collection
apex list map order collection
asked May 1 at 19:50
Brian KesslerBrian Kessler
1,6701234
1,6701234
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The order of iteration for both Map.keySet()
and Map.values()
are defined to be deterministic.
From the Summer '15 Release Notes, Iteration Order for Maps and Sets Is Now Predictable:
The order of elements in unordered collections (Map and Set) is now the same each time your code is run. Previously, the order of elements in unordered collections was arbitrary, and you couldn’t rely on the order of elements in maps and sets.
and from the Map
documentation under values()
:
The order of map elements is deterministic. You can rely on the order being the same in each subsequent execution of the same code. For example, suppose the values() method returns a list containing value1 and index 0 and value2 and index 1. Subsequent runs of the same code result in those values being returned in the same order.
It's important to note that both accessors are defined to be deterministic, but the specific deterministic order is not committed. Currently, it is order of addition to the collection for both pieces. This can be see via, e.g.,
Map<String, String> m = new Map<String, String>();
m.put('1', 'a');
m.put('2', 'b');
for (String s: m.values())
System.debug(s);
for (String s: m.keySet())
System.debug(s);
outputting 'a', 'b', '1', '2'.
While it seems unlikely that this behavior would change such that the order of iteration would be different between the two, I don't believe it's ever explicitly guaranteed to be the same.
This is mostly what I am looking for, but I'm a little hazy on 'it is order of addition to the collection for both pieces' ... Let's say that the same key is put multiple times. So, for example, '1' might be the first key. But the value associated with that key changes. Will the new value be promoted to the head of the list? Or will the recycled key be demoted match the latter put?
– Brian Kessler
May 1 at 20:07
2
@BrianKessler I wouldn't necessarily expect the order of the set to match the order of the values. I would use one or the other. Honestly, a loop is still your best option.
– sfdcfox
May 1 at 21:15
2
I personally would not feel comfortable building much that relies on that level of implementation detail where Salesforce has not made public commitments about the underlying mechanics.
– David Reed♦
May 1 at 21:16
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "459"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f260773%2fdo-maps-have-an-reliable-relationship-between-keyset-order-and-values-order%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The order of iteration for both Map.keySet()
and Map.values()
are defined to be deterministic.
From the Summer '15 Release Notes, Iteration Order for Maps and Sets Is Now Predictable:
The order of elements in unordered collections (Map and Set) is now the same each time your code is run. Previously, the order of elements in unordered collections was arbitrary, and you couldn’t rely on the order of elements in maps and sets.
and from the Map
documentation under values()
:
The order of map elements is deterministic. You can rely on the order being the same in each subsequent execution of the same code. For example, suppose the values() method returns a list containing value1 and index 0 and value2 and index 1. Subsequent runs of the same code result in those values being returned in the same order.
It's important to note that both accessors are defined to be deterministic, but the specific deterministic order is not committed. Currently, it is order of addition to the collection for both pieces. This can be see via, e.g.,
Map<String, String> m = new Map<String, String>();
m.put('1', 'a');
m.put('2', 'b');
for (String s: m.values())
System.debug(s);
for (String s: m.keySet())
System.debug(s);
outputting 'a', 'b', '1', '2'.
While it seems unlikely that this behavior would change such that the order of iteration would be different between the two, I don't believe it's ever explicitly guaranteed to be the same.
This is mostly what I am looking for, but I'm a little hazy on 'it is order of addition to the collection for both pieces' ... Let's say that the same key is put multiple times. So, for example, '1' might be the first key. But the value associated with that key changes. Will the new value be promoted to the head of the list? Or will the recycled key be demoted match the latter put?
– Brian Kessler
May 1 at 20:07
2
@BrianKessler I wouldn't necessarily expect the order of the set to match the order of the values. I would use one or the other. Honestly, a loop is still your best option.
– sfdcfox
May 1 at 21:15
2
I personally would not feel comfortable building much that relies on that level of implementation detail where Salesforce has not made public commitments about the underlying mechanics.
– David Reed♦
May 1 at 21:16
add a comment |
The order of iteration for both Map.keySet()
and Map.values()
are defined to be deterministic.
From the Summer '15 Release Notes, Iteration Order for Maps and Sets Is Now Predictable:
The order of elements in unordered collections (Map and Set) is now the same each time your code is run. Previously, the order of elements in unordered collections was arbitrary, and you couldn’t rely on the order of elements in maps and sets.
and from the Map
documentation under values()
:
The order of map elements is deterministic. You can rely on the order being the same in each subsequent execution of the same code. For example, suppose the values() method returns a list containing value1 and index 0 and value2 and index 1. Subsequent runs of the same code result in those values being returned in the same order.
It's important to note that both accessors are defined to be deterministic, but the specific deterministic order is not committed. Currently, it is order of addition to the collection for both pieces. This can be see via, e.g.,
Map<String, String> m = new Map<String, String>();
m.put('1', 'a');
m.put('2', 'b');
for (String s: m.values())
System.debug(s);
for (String s: m.keySet())
System.debug(s);
outputting 'a', 'b', '1', '2'.
While it seems unlikely that this behavior would change such that the order of iteration would be different between the two, I don't believe it's ever explicitly guaranteed to be the same.
This is mostly what I am looking for, but I'm a little hazy on 'it is order of addition to the collection for both pieces' ... Let's say that the same key is put multiple times. So, for example, '1' might be the first key. But the value associated with that key changes. Will the new value be promoted to the head of the list? Or will the recycled key be demoted match the latter put?
– Brian Kessler
May 1 at 20:07
2
@BrianKessler I wouldn't necessarily expect the order of the set to match the order of the values. I would use one or the other. Honestly, a loop is still your best option.
– sfdcfox
May 1 at 21:15
2
I personally would not feel comfortable building much that relies on that level of implementation detail where Salesforce has not made public commitments about the underlying mechanics.
– David Reed♦
May 1 at 21:16
add a comment |
The order of iteration for both Map.keySet()
and Map.values()
are defined to be deterministic.
From the Summer '15 Release Notes, Iteration Order for Maps and Sets Is Now Predictable:
The order of elements in unordered collections (Map and Set) is now the same each time your code is run. Previously, the order of elements in unordered collections was arbitrary, and you couldn’t rely on the order of elements in maps and sets.
and from the Map
documentation under values()
:
The order of map elements is deterministic. You can rely on the order being the same in each subsequent execution of the same code. For example, suppose the values() method returns a list containing value1 and index 0 and value2 and index 1. Subsequent runs of the same code result in those values being returned in the same order.
It's important to note that both accessors are defined to be deterministic, but the specific deterministic order is not committed. Currently, it is order of addition to the collection for both pieces. This can be see via, e.g.,
Map<String, String> m = new Map<String, String>();
m.put('1', 'a');
m.put('2', 'b');
for (String s: m.values())
System.debug(s);
for (String s: m.keySet())
System.debug(s);
outputting 'a', 'b', '1', '2'.
While it seems unlikely that this behavior would change such that the order of iteration would be different between the two, I don't believe it's ever explicitly guaranteed to be the same.
The order of iteration for both Map.keySet()
and Map.values()
are defined to be deterministic.
From the Summer '15 Release Notes, Iteration Order for Maps and Sets Is Now Predictable:
The order of elements in unordered collections (Map and Set) is now the same each time your code is run. Previously, the order of elements in unordered collections was arbitrary, and you couldn’t rely on the order of elements in maps and sets.
and from the Map
documentation under values()
:
The order of map elements is deterministic. You can rely on the order being the same in each subsequent execution of the same code. For example, suppose the values() method returns a list containing value1 and index 0 and value2 and index 1. Subsequent runs of the same code result in those values being returned in the same order.
It's important to note that both accessors are defined to be deterministic, but the specific deterministic order is not committed. Currently, it is order of addition to the collection for both pieces. This can be see via, e.g.,
Map<String, String> m = new Map<String, String>();
m.put('1', 'a');
m.put('2', 'b');
for (String s: m.values())
System.debug(s);
for (String s: m.keySet())
System.debug(s);
outputting 'a', 'b', '1', '2'.
While it seems unlikely that this behavior would change such that the order of iteration would be different between the two, I don't believe it's ever explicitly guaranteed to be the same.
answered May 1 at 19:56
David Reed♦David Reed
41.4k82463
41.4k82463
This is mostly what I am looking for, but I'm a little hazy on 'it is order of addition to the collection for both pieces' ... Let's say that the same key is put multiple times. So, for example, '1' might be the first key. But the value associated with that key changes. Will the new value be promoted to the head of the list? Or will the recycled key be demoted match the latter put?
– Brian Kessler
May 1 at 20:07
2
@BrianKessler I wouldn't necessarily expect the order of the set to match the order of the values. I would use one or the other. Honestly, a loop is still your best option.
– sfdcfox
May 1 at 21:15
2
I personally would not feel comfortable building much that relies on that level of implementation detail where Salesforce has not made public commitments about the underlying mechanics.
– David Reed♦
May 1 at 21:16
add a comment |
This is mostly what I am looking for, but I'm a little hazy on 'it is order of addition to the collection for both pieces' ... Let's say that the same key is put multiple times. So, for example, '1' might be the first key. But the value associated with that key changes. Will the new value be promoted to the head of the list? Or will the recycled key be demoted match the latter put?
– Brian Kessler
May 1 at 20:07
2
@BrianKessler I wouldn't necessarily expect the order of the set to match the order of the values. I would use one or the other. Honestly, a loop is still your best option.
– sfdcfox
May 1 at 21:15
2
I personally would not feel comfortable building much that relies on that level of implementation detail where Salesforce has not made public commitments about the underlying mechanics.
– David Reed♦
May 1 at 21:16
This is mostly what I am looking for, but I'm a little hazy on 'it is order of addition to the collection for both pieces' ... Let's say that the same key is put multiple times. So, for example, '1' might be the first key. But the value associated with that key changes. Will the new value be promoted to the head of the list? Or will the recycled key be demoted match the latter put?
– Brian Kessler
May 1 at 20:07
This is mostly what I am looking for, but I'm a little hazy on 'it is order of addition to the collection for both pieces' ... Let's say that the same key is put multiple times. So, for example, '1' might be the first key. But the value associated with that key changes. Will the new value be promoted to the head of the list? Or will the recycled key be demoted match the latter put?
– Brian Kessler
May 1 at 20:07
2
2
@BrianKessler I wouldn't necessarily expect the order of the set to match the order of the values. I would use one or the other. Honestly, a loop is still your best option.
– sfdcfox
May 1 at 21:15
@BrianKessler I wouldn't necessarily expect the order of the set to match the order of the values. I would use one or the other. Honestly, a loop is still your best option.
– sfdcfox
May 1 at 21:15
2
2
I personally would not feel comfortable building much that relies on that level of implementation detail where Salesforce has not made public commitments about the underlying mechanics.
– David Reed♦
May 1 at 21:16
I personally would not feel comfortable building much that relies on that level of implementation detail where Salesforce has not made public commitments about the underlying mechanics.
– David Reed♦
May 1 at 21:16
add a comment |
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f260773%2fdo-maps-have-an-reliable-relationship-between-keyset-order-and-values-order%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown