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
TapasviniTapasvini 

Inserting values in map

I want add the values from diff. list into single map.one is integer list and other is string list like:

List1: mapstr :my, hemant, free, Adult Content Or Keyord - my wife, Adult Content Or Keyword - ,my ,wife ,say ,try, Adult Content Or Keyword - ,my ,wife, my my, Adult Content Or mywife, asas

List2: mapint :[11, 6, 6, 1, 1, 1, 1, 1, 2]

but when I insert it into map it doesn't take all values.both list are of same size. and list contains only 4 or 5 values.

my code is :

public List<String> gettotaldetail() {
for(integer n=0;n<mapstr.size();n++)
{
String kw=mapstr.get(n);
integer cow=mapint.get(n);
map1.put(cow,kw);
}
str1=map1.values();
return str1;
}

what should be added to get the full list in map?

Jia HuJia Hu

In the Map, the key should be unique,

if you use List 2 as the key  List2: mapint :[11, 6, 6, 1, 1, 1, 1, 1, 2]

 

Only one copy of 1 and 6 will be left as the key.

ignatiuz_forceignatiuz_force

you should do the following to have all the values in the map

map<integer,list<String>> mapOfList=new map<integer,list<String>>()

for(integer n=0;n<mapstr.size();n++){
                                                
                 if(mapOfList.get(n)==null)
                    mapOfList.put(mapint.get(n),new list<String>());
                   mapOfList.get(mapint.get(n).add(mapstr.get(n));
                
 }

 

Then you can get all the values from the map by mapOfList.values();

 

Ignatiuz

TapasviniTapasvini

I have to sort it according to the key in descending order. So after sorting will it give related value only as it contins duplicate keys.? and then I want only top 5 values from it. how can I code it??

TapasviniTapasvini

your code is giving this error: Compile Error:

Method does not exist or incorrect signature: [Integer].add(String) at line 14 column 48