function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
naveen darshanalanaveen darshanala 

How to create a Map to store both string and Integer?

I have requirement where I need to create a map with values can be either string or Integer. Could someone please let me know how to approach?
Ajay K DubediAjay K Dubedi
Hi Naveen,

*You can Create map of both String and Integer, just keep sure that your key is Unique.

 Note: A map is a collection of key-value pairs where each unique key maps to a single value

  Map<integer,String> integer_String_Map = New Map<Integer,String>();
         ::
         ::
      <Key,Value>(pair)
* You can access string using integer or you can reverse the map if you need that way:
 
  Map<String,Integer> integer_String_Map = New Map<String,Integer>();
          ::
            ::
      <Key,Value>(pair)
 
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Prady01Prady01
map<string, integer> map = new map<string, integer>();

for(integer i=0; i<10; i++){
 map.put(string.valueOf(i), i);
}

 
naveen darshanalanaveen darshanala
All,  
I am not expecting this answer, hope you didnt get my question,
I need to create map in such a way that - in value field it should accomodate both string and integer., imy scenario is like i need to store the values coming from order object, 
so for example , i need to store name: which is  string
and then i need to store total amount which is a decimal.. etc etc
 
Deepali KulshresthaDeepali Kulshrestha
Hi naveen,

I have gone through your problem please refer bellow code:

Apex class:- 

public class TestMap {
    public static void maptest(){
        Map<Integer,String> lmap=new Map<Integer,String>();
        lmap.put(1,'Abc');
        lmap.put(2,'XYZ');
        System.debug(lmap);
    }

}



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Игорь ЖитченкоИгорь Житченко
You need to use type Object like second parameter in map. Map<String,Object>