function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

why is list ordered and map/set are unordered

why is list ordered and map/set are unordered??
can someone explain in detail.i know that list is ordered and set is unordered
An ordered collection means that the elements of the collection have a specific order. The order is independent of the value. A List is an example.

A sorted/unordered collection means that not only does the collection have order, but the order depends on the value of the element. A SortedSet is an example.

In contrast, a collection without any order can maintain the elements in any order. A Set is an example.

If that answers your query, please mark this as the best answer
hi rohit,,,thanks for the answer.

i know the difference between ordered and  unordered..but my question is why  is MAP an unordered collection and list is ordered collection???

This is because the map itself is a set with a reference to an object.  Since a set is unordered a map is similarly unordered.  For example you do myMap.keySet() to get all of the keys.  If you need to have an "ordered map" you can keep all of your keys in a list and then call them from the map

List<String> myKeys = new List<String>{'a', 'b', 'c'};
Map<String, String> myMap = new Map<String, String>{
     'b' => 'bar',
     'a' => 'apple',
     'c' => 'cat'

for (String key: myKeys) {
     String value = myMap.get(key);
     //Do something

Kiran Kumar 690Kiran Kumar 690
List is a ordered collection, meaning that each element is stored in a specific position. An element can be accessed using its location (also called as Index)

A set/map is an unordered collection.
Elements in a set are not stored in a specific order and you cannot access an element using an index
Felix van Hove 22Felix van Hove 22
By the time of writing of the last reply, the last reply was wrong and the question outdated. Apex sets, including map key sets, are ordered. See here (