Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to create a Map with duplicate Key

Key          Value
Product 1    100
product 1    200
product 2    200
product 2    300

Scenario : I have a trigger on quote and have to find the listprice of quotelineitems which have the same product name

I constructed a map with (productname,quotelineitem.listprice)

I have custom setting to check if the map has the key "product name" and then add up all the values

The problem is that I cannot form the above map because I am forgetting the fact that maps cannot have duplicate KEYS :(

IS there a better way to approach this

share|improve this question
up vote 6 down vote accepted

Map of lists maybe?

List<Account> fakeQuotes = new List<Account>{
    new Account(Name = 'Prod 1', AnnualRevenue = 100),
    new Account(Name = 'Prod 2', AnnualRevenue = 200),
    new Account(Name = 'Prod 1', AnnualRevenue = 300),
    new Account(Name = 'Prod 2', AnnualRevenue = 400),
    new Account(Name = 'Prod 1', AnnualRevenue = 500)

Map<String, List<Double>> listPrices = new Map<String, List<Double>>();
for(Account a : fakeQuotes){
    List<Double> temp = listPrices.get(a.Name);
    if(temp == null) {
        listPrices.put(a.Name, new List<Double>{a.AnnualRevenue});
    } else {

System.debug(listPrices); // {Prod 1=(100.0, 300.0, 500.0), Prod 2=(200.0, 400.0)}
share|improve this answer
The above case worked for me :) !!! Thanks @eyescream – Rao Jan 31 '13 at 19:45

Wouldn't it just be easier to create a Map with a List. Something along the lines of:


If you are looping over the products:

Map<String,List<Integer>> prodMap = new Map<String, List<Integer>>();
for(sObject prod:products){
    if(prodMap.get(prod.Name) != null){
        List<Integer> listOfValues = new List<Integer>();
        prodMap.put(prod.Name, listOfValues);

To add them up you would just then use

List<Integer> values = prodMap.get('name');
Integer finalValue = 0;
for(Integer val:values){
    finalValue += val;

finalValue should then equal the sum of all the values of the product named 'name'.

share|improve this answer
Wonderful solution, thank you! – Arthlete Mar 24 at 0:09

Use a map of maps! And iterate over the keys like so:


How to iterate over this map:

Integer i = 0;
List<sObject> products = new List<sObject>(); // assuming this is an sObject of some kind???
Map<String,Map<String,Integer>> productValuesByProduct = new Map<String,Map<String,Integer>>();

// build up the map of maps
for(sObject product: products){
    Map<String,Integer> valueEntry = new Map<String,Integer>();
    i++; // placeholder to avoid key collision

Again without knowing a lot about your data structures kind of hard to tell... but you can reverse where you put the iterator (i) on either key to loop over inserting multiple values

share|improve this answer
Will give this a try never thought on this line !!! – Rao Jan 31 '13 at 16:47
Can you post an example script for the above map I am trying to understand but saturated and not able to think more :( – Rao Jan 31 '13 at 17:00
Or probably use map<prod name, list<price>>. Use the map.Keyset.containskey method to see if you have prod name already present, if yes add the price to the corresponding list – Shailesh Deshpande Jan 31 '13 at 18:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.