Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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

3 Answers 3

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 {
        temp.add(a.AnnualRevenue);
    }
}

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

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

Map<Key,Map<Value-Key,Value>

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>();
    valueEntry.put('Value',product.get('value'));   
    productValuesByProduct.put(product.get('Name')+String.valueOf(i)),valueEntry);
    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
2  
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

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

Map<String,List<Integer>>

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){
        prodMap.get(prod.Name).add(prod.value);
    }else{
        List<Integer> listOfValues = new List<Integer>();
        listOfValues.add(prod.value);
        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

Your Answer

 
discard

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.