I have a user defined object called Employee like below and have two different lists which contains the Employee objects.
In the two different lists I need to findout unique object based on the name field in the object. Final list should contain only one object which the name is c.
Please suggest me how to do it using java 8?
import java.util.ArrayList;
import java.util.List;
class Employee{
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
public class UniqueObjects {
public static void main(String[] args) {
List<Employee> empOneList= new ArrayList<Employee>();
List<Employee> empTwoList= new ArrayList<Employee>();
Employee empOne= new Employee();
empOne.setName("a");
empOne.setAge("23");
empOneList.add(empOne);
Employee emptwo= new Employee();
emptwo.setName("b");
emptwo.setAge("24");
empOneList.add(emptwo);
Employee em= new Employee();
em.setName("a");
em.setAge("23");
empTwoList.add(em);
Employee emp1= new Employee();
emp1.setName("d");
emp1.setAge("24");
empTwoList.add(emp1);
}
}
c, when neither of the two starting lists have this name?cand not the list