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
SK R.ax1448SK R.ax1448 

Use of Maps in Apex

Hi All,

 

Any one can please explain what is the use/significance of Map(s) in Apex.  And what can do with maps & can not do without maps.

 

Thanks a ton in advance !

 

 

SK.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Sonu Sharma.ax1610Sonu Sharma.ax1610

Hi SK,

 

A map is a collection of key-value pairs where each unique key maps to a single value. Keys and values can be any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.

 

By using maps you can get the values of corresponding key without iterating.

 

For example

Account myAcct=new Account(); //Define a new account

Map<Integer, Account> m = new Map<Integer, Account>(); // Define a new map

m.put(1, myAcct); // Insert a new key-value pair in the map

System.assert(!m.containsKey(3)); // Assert that the map contains a key

Account a = m.get(1);

 

Using this way you are reducing the number of executable lines.

 

If you use List, you have to iterate over the list.

 

 

 

All Answers

Sonu Sharma.ax1610Sonu Sharma.ax1610

Hi SK,

 

A map is a collection of key-value pairs where each unique key maps to a single value. Keys and values can be any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.

 

By using maps you can get the values of corresponding key without iterating.

 

For example

Account myAcct=new Account(); //Define a new account

Map<Integer, Account> m = new Map<Integer, Account>(); // Define a new map

m.put(1, myAcct); // Insert a new key-value pair in the map

System.assert(!m.containsKey(3)); // Assert that the map contains a key

Account a = m.get(1);

 

Using this way you are reducing the number of executable lines.

 

If you use List, you have to iterate over the list.

 

 

 

This was selected as the best answer
SK R.ax1448SK R.ax1448

Thank you very much for the explaination.

 

SK