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
Soumya sri yaravaSoumya sri yarava 

how to check the contains key word in the List Collection in salesforce

hi, i already have a Duplicate list<string>, with 'A'  and 'C'..,
and dymaniclist<string>'A','B','C' 

1)if i select a and c  list<boolean> should be =[true, true]; // a and c   already in duplicate list 
2)if i select a and b list<bolean> should be =[true, false]; // a is in duplicate list 
3)if i select a and b and  c list<bolean> should be =[true, false,true]; // and c in the duplicate list
 
<apex:page controller="test3">
   <apex:form >
   <apex:pageBlock >
      <apex:pageblockTable value="{!wraplist}"  var="a">
       <apex:column ><apex:inputCheckbox value="{!a.selected}"/>{!a.name}</apex:column>
       </apex:pageblockTable>
    <apex:commandButton value="OrderSelected"  reRender="one,two" action="{!duplicatecheck}"/>
      <apex:outputLabel id="one">{!listSelectedNames}</apex:outputLabel>
       <apex:outputLabel id="two">{!Selectedbooleanlist}</apex:outputLabel>
    </apex:pageBlock>
   </apex:form>
</apex:page>
 
public class test3{
 public list<string>listSelectedNames{get;set;}
 public list<Wrapperclass>wraplist{get;set;}
 public list<boolean> Selectedbooleanlist{get;set;}
 public list<string>duplicatevalues{get;set;}
  
  public test3(){
 listSelectedNames = new list<string>();
 Selectedbooleanlist= new list<boolean>();
 duplicatevalues= new list<string>{'a','c'}; //duplicated to filter
 wraplist = new list<Wrapperclass>();
   Wrapperclass   wf = new Wrapperclass();
                  wf.selected=false;
                  wf.name ='a';
                 
    Wrapperclass   wf1 = new Wrapperclass();
                  wf1.selected=false;
                  wf1.name ='b';
                                  
   Wrapperclass   wf2 = new Wrapperclass();
                  wf2.selected=false;
                  wf2.name ='c';
                
          wraplist.add(wf);
          wraplist.add(wf1);
          wraplist.add(wf2);
 }
 
 
   public void duplicatecheck(){
   listSelectedNames.clear();
   Selectedbooleanlist.clear();
    for(wrapperclass wrap: wraplist){
      if(wrap.selected == true){
    listSelectedNames.add(wrap.name);
    Selectedbooleanlist.add(wrap.selected);
        }  
      }
     
     //to check duplicate
     for(String s1 : duplicatevalues){
      if(listSelectedNames.contains(s1)){
         boolean selected =true;
    // Selectedbooleanlist.add(selected);
      //add logic 
       }
    } 
        
        }
 //wrapper classs
    public class Wrapperclass{
       public boolean selected{get;set;}
       public string Name{get;set;}
    }
  
  
}

 
ShirishaShirisha (Salesforce Developers) 

Hi Sowmya,

Greetings!


Can you please check the below blog for sample code which might help you in achieving your requirement.

https://intellipaat.com/community/3207/check-if-an-apex-list-contains-an-object

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri