ONE DAY SALE

Free Salesforce Developers Tutorials >

Chapter 3 - Apex Datatypes and Operators >

Map Initialization Methods in APEX

Map Initialization Methods in APEX

What You’ll Learn


S2 Labs

Initialisation Of Map

Map<Integer, String> m = new Map<Integer, String>{5=>’Kalpana’};
m.put(1, ‘Bhavna’);
m.put(2, ‘Sapna’);
m.put(3, ‘Divya’);
m.put(4, ‘Bhavya’);
m.put(2, ‘Divya’); // this will override previous value

Map Of List In Apex

Map<Integer, List<Integer>> m = new Map<Integer, List<Integer>>();
m.put(1, new List());

Methods Of Map

put(key,value)Associates the specified value with the specified key in the map.Map<Integer,String> m = new
Map<Integer,String>();
m.put(1, ‘Bhavna’);m.put(2, ’Sapna’);
System.debug(s);//(1=‘Bhavna’,2=’Sapna’)
putAll(map)Copies all of the mappings from the specified map to the original map.Map<Integer,String> n = new
Map<Integer,String>();
n.put(3, ’Bhavya’);
n.put(4, ‘Divya’);
m.putAll(n);
System.debug(m);// (1=‘Bhavna’,2=’Sapna’
3=’Bhavya’,4=’Divya’)
get(key)Returns the value to which the specified key is mapped, or null if the map contains no value for this key.System.debug(m.get(1)); // ‘Bhavna’
System.debug(m.get(2)); // ’Sapna’
System.debug(m.get(3)); // ’Bhavya’
System.debug(m.get(4)); // ’Divya’
values()Returns a list that contains all the values in the map.List<String> l = new List<String>();
l = m.values();
System.debug(l);// (‘Bhavna’,’Sapna’,’Bhavya’,’Divya’)
clone()Makes a duplicate copy of the map.Map<Integer,String> o = new
Map<Integer,String>();
o = m.clone();
System.debug(o);// (1=‘Bhavna’,2=’Sapna’
3=’Bhavya’,4=’Divya’)
keySet()Returns a set that contains all of the keys in the map.System.debug(m.keySet());
// (1,2,3,4)
containsKey(key)Returns true if the map contains a mapping for the specified key.Boolean result1 = m.containsKey(3);
System.debug(result1); // true
Boolean result2 = m.containsKey(6);
System.debug(result2); // false
isEmpty()Returns true if map has 0 key.Boolean result = m.isEmpty();
System.debug(result); // false
size()Returns the number of key-value pairs in the map.System.debug(m.size()); // 4
remove(key)Removes the mapping for the specified key from the map, if present, and returns the corresponding value.m.remove(4);
System.debug(m);//(1=‘Bhavna’,2=’Sapna’3=’Bhavya’)
clear()Removes all of the key-value mappings from the map.m.clear();

Download Study Material

Get access to exclusive study material for Salesforce Certification and ace your exams!

Download Now

Our Salesforce Certification Courses

Hey there! Glad you made it through our Salesforce Developer Training for beginners . But wait! We've got some high-in-demand Salesforce courses for you to take your Salesforce skills to the next level, making you a desired professional in the Salesforce job market.

Post a Comment

Your email address will not be published. Required fields are marked *