Android - How to combine two ArrayLists

I have two ArrayLists created from parsed html. The first contains tasks and is similar to

Job A
Job B
Job C

      

and the second - how

Company A
Company B
Company C

      

I need a combination of Job A and Company A etc., so I can get results like (ArrayList would be great too)

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

      

I haven't found a clear tutorial or anything else. Any ideas?

+3


source to share


5 answers


Are you sure you are looking for the correct data structure for this?

Why not use 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 can even do something like this: (Change the lines to match 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 the company as your key and you can set multiple jobs for the company

+5


source


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 many ways to combine references between the two types of objects. Here is a flexible example that will allow you to use it to find another. It's too difficult if you know what you will always use to search. Usage LinkedHashMap

also preserves the order of insertion. So if you choose to put them in B, C, A, you can get them in the order B, C, A.



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);
}

      

+1


source


If you don't want to use a map (not sure why you did that) my approach would be: To create another class (call CompanyJob) that will contain both the company and the Job attribute, a set of CompanyJob instances (for example, ArrayList).

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++;
}

      

0


source


If you really want to store the combined values ​​in 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 element it will be O (n), but I assume you know this before deciding to go from ArrayList

.

0


source


You need to create a new one

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

      

You also need a counter.

int position = 0;

      

Then we iterate over the list (given that the size is the same for both lists).

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
}

      

-1


source







All Articles