anusha(salesforce developer)

interview Q&A on Collections




  1. List (ordered and allow duplicates)
  2. Set (unordered and won't allow duplicates)
  3. Map (Key and value pair)


1.What is the difference between map and set in Salesforce collections.
Map:
A map is a collection of key-value pairs where each unique key maps to a single value. Keys can be any primitive data type, while values can be a primitive, sObject, collection type or an Apex object. For example, the following table represents a map of countries and currencies:
Country (Key) 'United States' 'Japan' 'France' 'England' 'India'
Currency (Value) 'Dollar' 'Yen' 'Euro' 'Pound' 'Rupee'
Similar to lists, map values can contain any collection, and can be nested within one another. For example, you can have a map of Integers to maps, which, in turn, map Strings to lists. A map can only contain up to five levels of nested collections inside it.

Set:
A set is an unordered collection of primitives or sObjects that do not contain any duplicate elements. For example, the following table represents a set of String, that uses city names:
'San Francisco' 'New York' 'Paris' 'Tokyo' To declare a set, use the Set keyword followed by the primitive data type name within <> characters. For example:
http://www.salesforce.com/us/developer/docs/apexcode/index.htm

No limit for the size of a list. It only depends on the heap size which is 6 MB (Synchronous) and 12 MB (Asynchronous).
ListSet
List is Ordered.Set is unordered.
List allows duplicates.Set doesn't allow duplicates.
We can access list elements with index.Set elements cannot be accessed with index.
We can sort list elements with sortmethod (default sorting order is ascending).sort method is not available for Set.
Contains method is not available in List.Contains method is available for Set to search for a particular element in the set.
We can process records which are stored in list using DML statements(insert, update, delete and undelete).We cannot process recordswhich are stored in set usingDML statements.
What are the map methods available in Apex?
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Map holds key and value pair.
//Syntax: Map<datatype,datatype> mapName = new Map<datatype,datatype>();
/*Map<string,string> countryISTCodeMap = new Map<string,string>();
countryISTCodeMap.put('India','91');
countryISTCodeMap.put('USA','001');
countryISTCodeMap.put('India','911');//replaces old value with new value.*/
Map<string,string> countryISTCodeMap = new Map<string,string>{'India'=>'91','USA'=>'001', 'India'=>'911'};
system.debug('countryISTCodeMap result: '+countryISTCodeMap+' with size '+countryISTCodeMap.size());
system.debug('countryISTCodeMap keys: '+countryISTCodeMap.keyset());
system.debug('countryISTCodeMap values: '+countryISTCodeMap.values());
system.debug('countryISTCodeMap search: '+countryISTCodeMap.containsKey('India'));
system.debug('countryISTCodeMap fetching value based on key: '+countryISTCodeMap.get('India'));
//map keys are case-sensitive.
</string,string></string,string></string,string></string,string></datatype,datatype></datatype,datatype>
  1. keyset(): To fetch only keys from the map.
  2. values(): To fetch only values from the map.
  3. containsKey(value): To search a key from the map.
  4. get(key): By supplying the key we can fetch the value.
  5. put(key,value): To add key and value in a map.

No comments:

Post a Comment