You need to sign in to do that
Don't have an account?
chaitanyakuamar Teru
apex develoer
1=list<String> firstcolourlist=new list<String>{'red','blue','yellow','orange','blue','red'};
2= list<String>secondcolourlist=new list<String>{'blue','black','yellow','green','blue'};
print the common values
2= list<String>secondcolourlist=new list<String>{'blue','black','yellow','green','blue'};
print the common values
Please try the below code
Mark, it as the best answers if you resolve the issue.
Thanks,
Foram Rana
bt my question is to compare both list's & print the common values along with their count
like
output be like ={blue=4,yellow=2,red=2}
Please execute the following lines of code to get the required output:
List<String> Flist = new list<String>{'red','blue','yellow','orange','blue','red'};
List<String> Slist = new list<String>{'blue','black','yellow','green','blue'};
List<String> CommonValueList = new list<String>();
for(String var : Flist){
if(Slist.contains(var) && !CommonValueList.contains(var)){
CommonValueList.add(var);
}
}
List<String> MergedList = new list<String>();
MergedList.addAll(Flist);
MergedList.addAll(Slist);
Map<String, List<String>> requiredMap = new Map<String, List<String>>();
for(String obj : MergedList){
if(requiredMap.containsKey(obj)){
requiredMap.get(obj).add(obj);
}else{
requiredMap.put(obj,new list<String>());
requiredMap.get(obj).add(obj);
}
}
// Commonvaluelist contains the common values and iterate it over map to get the count values
for(String x: CommonValueList){
System.debug(x +' : '+requiredMap.get(x).size());
}
I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.
Thanks and Regards,
Sachin Arora
www.sachinsf.com