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?
How do I tell my manager that his code review comment is wrong?
How important is people skills in academic career and applications?
How wide is a neg symbol, how to get the width for alignment?
Using a microphone from the 1930s
Is it possible to know which is the correct temperature range and speed for any model?
How might a mountain bowl form?
Why is "Vayechulu" said 3 times on Leil Shabbat?
If I readied a spell with the trigger "When I take damage", do I have to make a constitution saving throw to avoid losing Concentration?
What is the most remote airport from the center of the city it supposedly serves?
A mathematically illogical argument in the derivation of Hamilton's equation in Goldstein
In Avengers 1, why does Thanos need Loki?
I need a disease
What does a spell range of "25 ft. + 5 ft./2 levels" mean?
Is this homebrew life-stealing melee cantrip unbalanced?
How did Kirk identify Gorgan in "And the Children Shall Lead"?
Hyperlink on red background
Why Isn’t SQL More Refactorable?
Missing Piece of Pie - Can you find it?
Why wasn't the Night King naked in S08E03?
Why was the battle set up *outside* Winterfell?
Manager is threatening to grade me poorly if I don't complete the project
Does a card have a keyword if it has the same effect as said keyword?
how to overfit?
How do LIGO and VIRGO know that a gravitational wave has its origin in a neutron star or a black hole?
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 3 hours ago
Brian KesslerBrian Kessler
1,6701134
1,6701134
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
2 hours ago
1
@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
1 hour ago
1
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♦
1 hour ago
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
2 hours ago
1
@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
1 hour ago
1
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♦
1 hour ago
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
2 hours ago
1
@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
1 hour ago
1
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♦
1 hour ago
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 3 hours ago
David Reed♦David Reed
41k82463
41k82463
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
2 hours ago
1
@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
1 hour ago
1
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♦
1 hour ago
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
2 hours ago
1
@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
1 hour ago
1
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♦
1 hour ago
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
2 hours ago
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
2 hours ago
1
1
@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
1 hour ago
@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
1 hour ago
1
1
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♦
1 hour ago
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♦
1 hour ago
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