-2

I have a list of this sorts

List<Employee>emp = Arrays.asList(new Employee("Jack", 29), new Employee("Tom", 24));

class Employee {

   private String name;
   private Integer id;

}

I want to insert to Employee full name List as follows:

List<Employee>empFullName = Arrays.asList(new Employee("Jack Tom", 29));

class EmployeeFullName {
   
   private String fullName;
   private Integer id;

}

How can I merge the name fields in Employee to fullName in Employee List after combining the names? I want to use Java 8 for the solution.

Naman
  • 27,789
  • 26
  • 218
  • 353
Ayman Patel
  • 564
  • 2
  • 8
  • 23
  • 1
    How do you know which items belong together if there are more than two? – knittl May 18 '21 at 18:00
  • You're creating a new Employee object using the first names of two other employees. That does not make sense. – WJS May 18 '21 at 18:00
  • 1
    That makes no sense, at least to me. Why do you have two different classes? What should the desired result look like? – Eritrean May 18 '21 at 18:02

1 Answers1

0

Notwithstanding all the reasonable questions previous commenters have posted, it feels to me like your main problem boils down to "How do I get pairs of objects out of a stream".

Once you have paired up the objects into a new collection (or stream) of pairs, you can do whatever you want to with them (i.e. make a new object out of them).

Collect successive pairs from a stream

You would still have to decide how to "merge" the pairs. In your case, it looks like you're taking the "name" and joining them together for each Pair to produce a fullName. And you're using the left-hand-side ID. That still leaves one to wonder what happened to the right-hand-side ID, but maybe with your real data-set, it's functionally duplicated..? Even so, it might be worth doing a programmatic Assert to make sure Pairs you're streaming out are consistent in that way. Otherwise one missing element in your stream and you'll be tying together all sorts of random users...

Atmas
  • 2,389
  • 5
  • 13