+ Start a Discussion
John Neilan 18John Neilan 18 

Basic Question About Maps & Sets

Hi,

I have a pretty basic question (I think) about the use of a Set within a map.  I have a trigger with a piece of code:
 
map<Id, set<String>> newMap = new map<Id, set<String>>();

For(Custom_Object__c cust : Trigger.new){
newMap.put(cust.Contact__c, new set<String>());
}

I am trying to create a keyset of Contact ID & Type on a custom object so I can compare to ensure Contact records only allow 1 Type of each related custom object record.  When I run the Debug statements, the map is only populating the Contact ID.  I have 2 questions:

What are the set<string> in my map and new set<string> in my put statement designed to do, and how do I get the set<string> to populate?
Best Answer chosen by John Neilan 18
Abhishek BansalAbhishek Bansal
Hi John,

Set<String> is used to get the list of strings with unique values. If you try to add duplicate values it will override the existing value and at the end you will only get a list of string with unique values. This is the basic use of Set<String>.
In your code you will never get anything in this set because nothing was added to it. You should add the value of type field in this set using the below code of line:
newMap.get(cust.Contact__c).add(cust.Type__c);
Please replace the Type__c from the actual API name of the Type field from custom object.


By doing this you will get a map where key will be Contact Id and values will be list of unique Type values.

Let me know if you need any more information on this.

Thanks,
Abhishek Bansal.
Gmail: abhibansal2790@gmail.com
Skype: abhishek.bansal2790
Phone: +917357512102

All Answers

Abhishek BansalAbhishek Bansal
Hi John,

Set<String> is used to get the list of strings with unique values. If you try to add duplicate values it will override the existing value and at the end you will only get a list of string with unique values. This is the basic use of Set<String>.
In your code you will never get anything in this set because nothing was added to it. You should add the value of type field in this set using the below code of line:
newMap.get(cust.Contact__c).add(cust.Type__c);
Please replace the Type__c from the actual API name of the Type field from custom object.


By doing this you will get a map where key will be Contact Id and values will be list of unique Type values.

Let me know if you need any more information on this.

Thanks,
Abhishek Bansal.
Gmail: abhibansal2790@gmail.com
Skype: abhishek.bansal2790
Phone: +917357512102
This was selected as the best answer
John Neilan 18John Neilan 18
Thanks Abhishek! That makes sense now.
Sumit  61Sumit 61
Try somthing like this:

Map<Id, Set<String>> newMap = new Map<Id, Set<String>>();
for(Custom_Object__c cust : Trigger.new) {
    if(newMap.containsKey(cust.Contact__c)) {
        Set<String> stringValue = newMap.get(cust.String__c);
        stringValue.add(cust.String__c);
        newMap.put(cust.Id, stringValue);
    } else {
        newMap.put(cust.Id, new Set<Id> { cust.String__c });
    }
}