Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7068281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:18:17+00:00 2026-05-28T05:18:17+00:00

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

  • 0

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());
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:18:18+00:00Added an answer on May 28, 2026 at 5:18 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Clojure, I want to combine two lists to give a list of pairs,
I simply want to remove duplicates from two lists and combine them into one
Possible Duplicate: Clearest way to combine two lists into a map (Java)? Given this:
I want to combine two array's, excluding duplicates. I am using a custom class:
I have two List<int> instances. Now I want to combine them into a third
I have two lists that i need to combine where the second list has
We have two mailing lists that we need to combine, and then make sure
I want to combine two ArrayList into a single one. My first arraylist looks
I'd like to combine two lists. If I have the following two lists: {a,b,c,d}
Specifically, I have two lists of strings that I'd like to combine into a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.