+ Start a Discussion
sfdcFanBoysfdcFanBoy 

Simple Maps - logic please

Hey All,

 

I have 3 maps of type <String,Decimal>.  And the values are as below

 

m1 -  {x1,a1;  x2,a2;  x3,a3}

m2 -  {x1,b1;  x2,b2;  x3,b3}

m3 -  {x1,c1;  x2,c2;  x3,c3}

 

All the strings(keys) in all the maps are same. x1, x2, x3.

 

I want to make a new map M of type <String,decimal,decimal,decimal> and the values should be

 

{

x1,a1,b1,c1;

x2,a2,b2,c2;

x3,a3,b3,c3;

}

 

How to achieve this?  any logic please

Subhash GarhwalSubhash Garhwal

Hi Manish,

 

If i am not wrong than you want your map this way

 

x1 as key and a1,b1,c1 as value

 

for that you can use youe map like

 

Map<String,List<Decimal>> mapTest = new Map<String, List<Decimal>>();

 

Than put values like

 

  mapTest.put(x1, new List<Decimal>{a1,b1,c1});

  mapTest.put(x2, new List<Decimal>{a2,b2,c2});

  mapTest.put(x3, new List<Decimal>{a3,b3,c3}); 

 

Or if you want to populate youe map dynamically than you can go through this like

 

http://abhithetechknight.blogspot.in/2013/10/logic-for-maps-having-list-at-values.html

 

 

Thanks

 

Hit the Kudos button (star)  and Mark as solution if it post helps you

GlynAGlynA

You can declare the map this way:

 

Map<String,List<Decimal>> myMap = new Map<String,List<Decimal>>
{   x1 => new List<Decimal>{ a1, b1, c1 },
    x2 => new List<Decimal>{ a2, b2, c2 },
    x3 => new List<Decimal>{ a3, b3, c3 }
};

 -Glyn

sfdcFanBoysfdcFanBoy
Thanks for the replies.

But I do not want to use List<Decimal> as my list of decimals is fixed. Only 3 decimals. And this Map<String, Decimal, Decimal, Decimal> will be used as input for Visualforce charting.

So, please let me know how to dynamically put those individual maps m1,m2,m3 in the required format.

Thanks much!
GlynAGlynA

I also read the blog that Subash posted.  I prefer this method of dynamically populating a map of lists:

 

if ( !theMap.containsKey( theKey ) )
{
    theMap.put( theKey, new List<valueType>() );
}
theMap.get( theKey ).add( theValue );

Obviously, you have to replace "theMap", "theKey", "theValue" and "valueType" with the appropriate variables and type for your code, but the form is concise and efficient.

 

-Glyn

 

GlynAGlynA

A map can only have one value.  You can't create a Map<String,Decimal,Decimal,Decimal>.

 

If you don't want to use a List, you can create an inner class:

 

public class myValues
{
    public Decimal a {get; set;}
    public Decimal b {get; set;}
    public Decimal c {get; set;}

    public myValues( Decimal aa, Decimal bb, Decimal cc )
    {
        a = aa;  b = bb;  c = cc;
    }
}

Then create your map, and a way for the VF page to access the values of the map:

Map<String,myValues> myMap = new Map<String,myValues>
{   x1 => new myValues( a1, b1, c1 ),
    x2 => new myValues( a2, b2, c2 ),
    x3 => new myValues( a3, b3, c3 )
};

public List<myValues> myMapValues { get { return myMap.values(); } }

 In your VF page, you could use a pageBlockTable:

<apex:pageBlockTable value="{!myMapValues()}" var="theValues">
    <apex:column value="{!theValues.a}"/>
    <apex:column value="{!theValues.b}"/>
    <apex:column value="{!theValues.c}"/>
</apex:pageBlockTable>

Is this closer to what you are trying to do?

 

-Glyn

 

 

GlynAGlynA

Here's the code that uses the original maps, m1, m2 and m3:

Map<String,myValues> myMap = new Map<String,myValues>
{   x1 => new myValues( m1.get( x1 ), m2.get( x1 ), m3.get( x1 ) ),
    x2 => new myValues( m1.get( x2 ), m2.get( x2 ), m3.get( x2 ) ),
    x3 => new myValues( m1.get( x3 ), m2.get( x3 ), m3.get( x3 ) )
};

 

GlynAGlynA

Or you could do it more dynamically:

Map<String,myValues> myMap = new Map<String,myValues>();

for ( String x : new List<String>{ x1, x2, x3 } )
{
    myMap.put( x, new myValues( m1.get( x ), m2.get( x ), m3.get( x ) );
}

 Am I getting warm yet?

 

-Glyn