Java – Android – How to merge two ArrayLists

Android – How to merge two ArrayLists… here is a solution to the problem.

Android – How to merge two ArrayLists

I have two ArrayLists, which are created from parsed html. The first one contains the job, like

Job A
Job B
Job C

The second is like

Company A
Company B
Company C

What I need is a combination of job A and company A etc, so I can get similar results (ArrayList is great too).

Job A : Company A
Job B : Company B
Job C : Company C

I didn’t find a clear tutorial or anything like that. Any ideas?

Solution

Are you sure you’re looking for the right data structure to achieve this?

Why not use Map ? You can follow this route to define a key/value relationship.

Map<Company, Job> jobMap = new HashMap<Company, Job>();
jobMap.put("Company A" /* or corresponding list item */, "Job A" /* or corresponding list item */);

You can even do something like this: (swap the string for yours to suit your implementation).

Map<Company, List<Job>> jobMap...;
List<Job> jobList = new ArrayList<Job>();
jobList.add("Job A");
jobList.add("Job B");
jobList.add("Job C");
jobMap.put("Company A", jobList);

What this will do is define a company as your key, and you can set up multiple jobs for a company

Related Problems and Solutions