简体   繁体   中英

How do I combine two lists with the same amount of elements in Java?

How do I combine two Lists in Java? The output so far is:

Firstname1
Firstname2
Firstname3
Lastname1
Lastname2
Lastname3
[[Firstname1, Firstname2, Firstname3], [Lastname1, Lastname2, Lastname3]]

I want the out put to be:

[Firstname1 Lastname1, Firstname2 Lastname2, Firstname3 Lastname3}

import java.util.Arrays;
import java.util.List;
import java.util.Iterator;
import java.util.HashSet;

public class Main {

public static void main(String[] args) {
   List<String> peoplFname = Arrays.asList("Firstname1", "Firstname2", "Firstname3");
   List<String> peoplLname = Arrays.asList("Lastname1", "Lastname2", "Lastname3");
   Iterator<String> iterator = peoplFname.iterator();
   while(iterator.hasNext()) {
        System.out.println(iterator.next());
   }

   Iterator<String> iteratorx = peoplLname.iterator();
   while(iteratorx.hasNext()) {
       System.out.println(iteratorx.next());
   }

   HashSet peopleFullName = new HashSet();

   peopleFullName.add(peoplFname);
   peopleFullName.add(peoplLname);

   System.out.println(peopleFullName.toString());
}
}

Use addAll instead of add , in order to add all elements from the list into your set.

Change your code to:

peopleFullName.addAll(peoplFname);
peopleFullName.addAll(peoplLname);

Update:

Based on the updated question, it looks like you want to combine corresponding elements from both lists. You're on the right track. You just need to iterate over both lists, join the first name with the last name and then add it to a result list:

List<String> peoplFname = Arrays.asList("Firstname1", "Firstname2", "Firstname3");
List<String> peoplLname = Arrays.asList("Lastname1", "Lastname2", "Lastname3");

Iterator<String> iterator = peoplFname.iterator();
Iterator<String> iteratorx = peoplLname.iterator();

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

while(iterator.hasNext() && iteratorx.hasNext()) {
    String fullName = iterator.next() + " " + iteratorx.next();
    peopleFullName.add(fullName);
}

System.out.println(peopleFullName);

Since Java is an Object Orientated Language I would use Object with two fields firstName and lastName . This would make adding the two list together much simpler. You can add a toString method to you new class which would produce the output you want.

If I understand correctly, what you want is to concatenate the elements from both lists:

List<String> fullNames = new ArrayList<String>(firstNames.size());
for (int i = 0; i < firstNames.size(); i++) {
    fullNames.add(firstNames.get(i) + " " + lastNames.get(i));
}

Or, using iterators (which would be important if the lists were long lists not backed by an array):

List<String> fullNames = new ArrayList<String>(firstNames.size());
Iterator<String> lastNameIterator = lastNames.iterator();
for (Iterator firstNameIterator = firstNames.iterator(); firstNameIterator.hasNext();) {
    String firstName = firstNameIterator.next();
    String lastName = lastNameIterator.next();
    fullNames.add(firstName + " " + lastName);
}

That said, I agree with Peter's answer: you should use a Person object with two properties: firstName and lastName .

Side note: I renamed your variables to make the code much more readable.

ArrayList<String> peopleFullNames = new ArrayList<String>();

for(i = 0; i < peopleFName.length; i++){
    peoplNames.add(peopleFName.get(i) + " " + peopleLName.get(i));
}

Basically, this will create an ArrayList (or alternatively, you can create an array since you know the size) and then add the names to it one by one, combing the strings from both lists as you do it.

If you want to combine Firstname1 -> Lastname1; Firstname2 -> Lastname2....

You should do an simple for:

String[] fullName = new String[peoplFname.size()];
for(int i = 0; i < peoplFname.size(); i++)
{
    fullName[i] = peopleFname.get(i)+" "+peopleLname.get(i);
}

Considering that peopleFname and peopleLname have the same number of elements. If you want to make all possible name combinations, you should put an while inside the other one. So for the first name, all the last names will be iterated, and it'll happens for all the first names:

ArrayList<String> allCombinations = new ArrayList<String>();

Iterator<String> iterator = peoplFname.iterator();
while(iterator.hasNext()) {        
    Iterator<String> iteratorx = peoplLname.iterator();
    while(iteratorx.hasNext()) {
        allCombinations.add(iterator.next()+" "+iteratorx.next());
    }
}

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