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
harry63harry63 

How to combine two Lists

HI i have some selected contacts inone list and i have some selected contacts in other list.i want to display the selected contacts on a vf page. does any one tell me how to combine selected records in both lists.

It is very urgent

thanks in advance

bob_buzzardbob_buzzard

To combine lists you can simply create a new list and add both your existing lists to it:

 

 

// assuming list1 and list2 contain the data to combine

public List<Contact> getCombinedList()
{
   List<Contact> result=new List<Contact>();
   result.addAll(list1);
   result.addAll(list2);

   return result;
}

 

 

 

 

Pradeep_NavatarPradeep_Navatar

You can use the addall method as a more efficient way to combine your sublists, instead of the for loops.

 

                                                This is code for Lead object same way you can do this with contact object.

            List<Lead> L1 = new List<Lead>([select id,name,leadsource from Lead where leadsource='Phone Inquiry' limit 1] );

                                                List<Lead> L2 = new List<Lead>([select id,name,leadsource from Lead where leadsource='Purchased List' limit 1]);

                                                system.debug('$$$$$$$$$$$$$$$4'   +  L1[0].id);

                                                system.debug('@@@@@@@@@@@@@@@@@'   +  L2[0].id);

 

                                                List<Lead> MasterLead = new List<Lead>();

                                                MasterLead.addall(L1);

                                                MasterLead.addall(L2);

                                                system.debug('#######3 '  + MasterLead[1].id);

harry63harry63

Thans for reply.still not getting expected result.

contactlist and ccontactlist are the two lists containig some records.i want to add the selected records in that two lists.I am using wrapper class..Pleae help me very urgent

public void addcons()

{

{ integer j=0;

integer i=0; 

 

for(cContact cc:contactList)

{{contactlist1.add(cc); 

if(cc.selected==true)

}}

 

{{

contactlist2.add(scc);

 

 

for(ccontact scc:ccontactlist)

if(scc.selected==true)

}}

contactlist.clear();

contactlist.addall(contactlist1);

contactlist.addall(contactlist2);

 

note:contactlist is displaying only contactlist2 records.

harry63harry63

Thans for reply.still not getting expected result.

 

contactlist and ccontactlist are the two lists containig some records.i want to add the selected records in that two lists.I am using wrapper class..Pleae help me very urgent

 

public void addcons()

 

{

 

 

 

 

 

 

{ integer j=0;

 

integer i=0; 

 

 

 

for(cContact cc:contactList)

 

 

 

 

 

 

 

{{contactlist1.add(cc); 

 

if(cc.selected==true)

}}

 

 

 

 

 

 

 

 

{{

 

contactlist2.add(scc);

 

 

 

 

 

for(ccontact scc:ccontactlist)

 

 

if(scc.selected==true)

 

 

 

 

 

 

 

}}

 

contactlist.clear();

 

contactlist.addall(contactlist1);

 

contactlist.addall(contactlist2);

 

 

 

note:contactlist is displaying only contactlist2 records.

 

 

 

 

 

 

 

 

bob_buzzardbob_buzzard

Have you tried adding some debug to your method to output information about contactlist (e.g. size) once you've added in each of the "sublists"?  I can't see any reason why what you are doing isn't working.

Suraj Tripathi 47Suraj Tripathi 47
Hi harry63,

You can combine two lists using apex this way:
List<Opportunity> OppList = new List<Opportunity>();
OppList.addAll(list1);
OppList.addAll(list2);

If you find your Solution then mark this as the best answer. 

Thank you!

Regards 
Suraj Tripathi