Header Ads Widget

SFDC INTERVIEW QUESTIONS ON APEX PART2




· 1. What are the types of Collections available in Apex?
Collections are three types...
1. List [ordered and allow duplicates]
2. Set [unordered and won't allow duplicates]
3. Map [Key and value pair]




    2. What is the difference between List and Set ?


    S No
    List
    Set
    1
    List is Ordered.
    Set is unordered.
    2
    List allows duplicates.
    Set doesn't allow duplicates.
    3
    We can access list elements with index.
    Set elements cannot be accessed with index.
    4
    We can sort list elements with sort method (default sorting order is ascending).
    Sort method is not available for Set.
    5
    Contains method is not available in List.
    Contains method is available for Set to search for a particular element in the set
    6
    We can process records which are stored in list using DML statements (insert, update, delete and undelete).
    We cannot process records which are stored in set using DML statements.



    ·  3. What is the maximum size of the list?

    • · 
      It depends on the heap size which is 6 MB (Synchronous) and 12 MB (Asynchronous). No limit for the size of a list

    4.Collections in Salesforce


    Set:
           Collection or Group of primitive/Non–primitive data types.
           Set index starts from zero.
           List doesn’t allow any duplicate values.
           List is an Un-ordered list.
    Syntax:
    Set<primitive/non-primitive type>  setinstance = new set<primitive/non-primitive type> ();
    Ex:
     set<integer> setint = set<integer>();
     set<string> setstr = set<string>();
     set<Account>setacc = set<Account>();
     set<Employee__c> setemp = set< Employee__c >();

    Set Methods:
    The following methods are set methods. These are all instance methods.

    add(setElement): Adds an element to the set if it is not already present.
    Signature:

    public Boolean add(Object setElement)


    addAll(fromList): Adds all of the elements in the specified list to the set if they are not already present.
    Signature:

    public Boolean addAll(List<Object> fromList)


    addAll(fromSet): Adds all of the elements in the specified set to the set that calls the method if they are not already present.
    Signature:

    public Boolean addAll(Set<Object> fromSet)


    clear(): Removes all of the elements from the set.
    Signature:

    public Void clear()


    clone(): Makes a duplicate copy of the set.
    Signature:

    public Set<Object> clone()


    contains(setElement): Returns true if the set contains the specified element.
    Signature:

    public Boolean contains(Object setElement)


    containsAll(listToCompare): Returns true if the set contains all of the elements in the specified list. The list must be of the same type as the set that calls the method.
    Signature:

    public Boolean containsAll(List<Object> listToCompare)


    containsAll(setToCompare): Returns true if the set contains all of the elements in the specified set. The specified set must be of the same type as the original set that calls the method.
    Signature:

    public Boolean containsAll(Set<Object> SetToCompare)


    equals(set2): Compares this set with the specified set and returns true if both sets are equal; otherwise, returns false.
    Signature:

    public Boolean equals(Set<Object> set2)


    hashCode(): Returns the hashcode corresponding to this set and its contents.
    Signature:

    public Integer hashCode()


    isEmpty(): Returns true if the set has zero elements.
    Signature:

    public Boolean isEmpty()


    remove(setElement): Removes the specified element from the set if it is present.
    Signature:

    public Boolean remove(Object setElement)


    removeAll(listOfElementsToRemove): Removes the elements in the specified list from the set if they are present.
    Signature:

    public Boolean removeAll(List<Object> listOfElementsToRemove)


    removeAll(setOfElementsToRemove): Removes the elements in the specified set from the original set if they are present.
    Signature:

    public Boolean removeAll(Set<Object> setOfElementsToRemove)


    retainAll(listOfElementsToRetain): Retains only the elements in this set that are contained in the specified list.
    Signature:

    public Boolean retainAll(List<Object> listOfElementsToRetain)


    retainAll(setOfElementsToRetain): Retains only the elements in the original set that are contained in the specified set.
    Signature:

    public Boolean retainAll(Set setOfElementsToRetain)


    size(): Returns the number of elements in the set (its cardinality).
    Signature:

    public Integer size()



    Map:
           Map is collection of Key and Value Pair.
           Keys are unique, allows only primitive data types.
           Values are allowed duplicate values, it can be primitive or Non-Primitive data types or user-defined.
    Syntax:
    map<key, value> mapinstance = new map< key, value>();

    Ex:
    map<integer, string> mapis =new map<integer, string>();
    map<string, string> mapss =new map<string, string>();
    map<string, account> mapsa =new map<string, account>();
    map<Id, User> mapiu =new map<Id, User >();

    Map Methods:
    The following methods are Map methods. These are all instance methods.

    clear(): Removes all of the key-value mappings from the map.
    Signature:

    public Void clear()


    clone(): Makes a duplicate copy of the map.
    Signature:

    public Map<Object, Object> clone()


    containsKey(key): Returns true if the map contains a mapping for the specified key.
    Signature:

    public Boolean containsKey(Object key)


    deepClone(): Makes a duplicate copy of a map, including sObject records if this is a map with sObject record values.
    Signature:

    public Map<Object, Object> deepClone()


    equals(map2): Compares this map with the specified map and returns true if both maps are equal; otherwise, returns false.
    Signature:

    public Boolean equals(Map map2)


    get(key): Returns the value to which the specified key is mapped, or null if the map contains no value for this key.
    Signature:

    public Object get(Object key)


    getSObjectType(): Returns the token of the sObject type that makes up the map values.
    Signature:

    public Schema.SObjectType getSObjectType()


    hashCode(): Returns the hashcode corresponding to this map.
    Signature:

    public Integer hashCode()


    isEmpty():Returns true if the map has zero key-value pairs.
    Signature:

    public Boolean isEmpty()


    keySet(): Returns a set that contains all of the keys in the map.
    Signature:

    public Set<Object> keySet()


    put(key, value): Associates the specified value with the specified key in the map.
    Signature:

    public Object put(Object key, Object value)


    putAll(fromMap): Copies all of the mappings from the specified map to the original map.
    Signature:

    public Void putAll(Map fromMap)


    putAll(sobjectArray): Adds the list of sObject records to a map declared as Map<ID, sObject> or Map<String, sObject>.
    Signature:

    public Void putAll(sObject[] sobjectArray)


    remove(key): Removes the mapping for the specified key from the map, if present, and returns the corresponding value.
    Signature:

    public Object remove(Key key)


    size(): Returns the number of key-value pairs in the map.
    Signature:

    public Integer size()


    values(): Returns a list that contains all the values in the map.
    Signature:

    public List<Object> values()


      Post a Comment

      0 Comments