简体   繁体   中英

Android - How to combine two ArrayLists

I have got two ArrayLists, created from parsed html. First one contains jobs and is like

Job A
Job B
Job C

and the second one is like

Company A
Company B
Company C

What I need is combination of Job A and Company A and so on, so I can get the results like (an ArrayList too would be great)

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

I didn't find clear tutorial or something. Any ideas?

Are you sure you are looking at the correct data structure to achieve this?

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

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

You may even do something like this: (Swap out the strings to your to fit 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 multiple jobs to a company

if (jobs.length() != companies.length()) {
    throw new InvalidArgumentException("Mismatch of jobs and companies");
}

for (int i = 0; i < jobs.length(); i++) {
   combine(jobs.get(i), companies.get(i));
}

There are lots of ways to combine references between two kinds of objects. Here's a flexible example that will let you use one to look up the other. It's overkill if you know which you'd always be using to do the lookup. Using LinkedHashMap also preserves the insertion order. So if you decide to put them in B, C, A order, you can get them out in B, C, A order.

LinkedHashMap<Job, Company> jobToCompany = new LinkedHashMap<>();
LinkedHashMap<Company, Job> companyToJob = new LinkedHashMap<>();

private void combine(Job job, Company company) {
    jobToCompany.put(job, company);
    companyToJob.put(company, job);
}

If you're not willing to use a Map (not sure why you would that) my approach would be: To create another class (lets call it CompanyJob) that would contain both a Company and a Job attribute, then simply have a collection of your CompanyJob instances (an ArrayList would do).

class CompanyList{
  private Company mCompany;
  private Job mJob;
  public CompanyList (Company com, Job job){
    mCompany = com;
    mJob = job;
  }
  // Some logic ...
}
// Then your list
private ArrayList<CompanyList> yourList = new ArraList <>();
int i = 0;
for (Company tmpCom: companyList){
  yourList.add (new CompanyJob (tmpCom,jobList.get(i));
  i++;
}

If you really want to store the combined values in an ArrayList then the following code will work for you:

List<String> jobs = new ArrayList<>(); 
List<String> companies = new ArrayList<>();
List<String> mergedList = new ArrayList<>();
//assuming the value are populated for `jobs` and `companies`. 
if(jobs.size() == companies.size()) {
    int n = jobs.size();
    for(int index=0; index<n; index++)
    {
        mergedList.add(jobs.get(index) + " : " + companies.get(index))
    }
} else {
    System.out.println("Cannot combine");
    //Throw exception or take any action you need.
}

Keep in mind that if you need to search for any item it would be O(n) but I assume you are aware of it before taking decision of going with an ArrayList .

You need to create a new one

List<String> third = new ArrayList<String>();

Also need a counter.

int position = 0;

Then iterate through the list (considering the size is same for both the list).

for(String item:firstList){
third.add(item+ " : " + secondList.get(position);
position ++;
}

Then the third will have the desired result.

To confirm:

for (String item:third){
//try to print "item" here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM