I have two lists of custom objects. And I want to merge both lists by id, using Java 8.
I have a class Employee with the fields (All String): id, name, city.
And I have another class Person with the fields (All String): id, city.
Here is an example :
List<Employee> employeeList = Stream.of(
new Employee("100","Alex",""),
new Employee("200","Rida",""),
new Employee("300","Ganga",""))
.collect(Collectors.toList());
List<Person> personList = Stream.of(
new Person("100","Atlanta"),
new Person("300","Boston"),
new Person("400","Pleasanton"))
.collect(Collectors.toList());
After merging the two lists I want to get the result shown below.
How can I do it?
List<Employee>
[
Employee(id=100, name=Alex, city=Atlanta),
Employee(id=200, name=Rida, city=null),
Employee(id=300, name=Ganga, city=Boston),
Employee(id=400, name=null, city=Pleasanton)
]