0
for(String name : nameList  ){
    for(WrapperClass wrap : WrapListRun){
        if(name == wrap.acc.Name){
            accountWrapperList.add(wrap);
        }
        else if(name == wrap.con.Account.Name){
            accountWrapperList.add(wrap);
        }
    }
}
1

Create a Map and bind it with key as a name. Refer below code:

// populate a map to store wrapper class with key as name
Map<String, WrapperClass> mapWrapper = new Map<String,WrapperClass>();

for(String name : nameList) {
    WrapperClass wrap = mapWrapper.get(name);
    if(wrap.acc.Name.equals(name) {
        accountWrapperList.add(wrap);
    }else if(wrap.con.Account.Name.equals(name)) {
        accountWrapperList.add(wrap);
    }
}
1
  • 2
    +1 But don't use the "equals" method, as that is far more costly than simply "==". – sfdcfox Jul 4 '17 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy