Adding array of object into array list and compare with other array list based on one property value and filter the result.
I have an array of object personDetails and departmentDetails.
package com.education;
import java.util.*;
public class educationMain {
public static void main(String[] args) {
List<person> list=new ArrayList<person>();
person l1 = new person(1,"Samual",100,"Sales","Business");
person l2 = new person(2,"Alex",100,"Sales","Business");
person l3 = new person(3,"Bob",101,"Engineering","Technology");
person l4 = new person(4,"Michel",101,"Engineering","Technology");
person l5 = new person(5,"Ryan",102,"PR","Services");
person l6 = new person(6,"Horward",103,"Leadership","Managmnet");
person l7 = new person(7,"Cyna",104,"HR","Human Resource");
list.add(l1);
list.add(l2);
list.add(l3);
list.add(l4);
list.add(l5);
list.add(l6);
list.add(l7);
for(person b:list){
System.out.println(b.personId+" "+b.name+" "+b.deptCode+" "+b.parentDept+" "+b.deptName);
}
List<department> depList = new ArrayList<department>();
department d1 = new department(100, "Sales","Business");
department d2 = new department(101, "Engineering","Technology");
department d3 = new department(102, "PR","Services");
depList.add(d1);
depList.add(d2);
depList.add(d3);
for(department b:depList){
System.out.println(b.deptCode+" "+b.parentDept+" "+b.deptName);
}
}
}
So above code is working fine and displaying correctly.
My person class
package com.education;
public class person {
public int personId;
public String name;
private int deptCode;
private String parentDept;
private String deptName;
public person(int personId, String name, int deptCode, String parentDept, String deptName) {
super();
this.personId = personId;
this.name = name;
this.deptCode = deptCode;
this.parentDept = parentDept;
this.deptName = deptName;
}
public void display(){
System.out.println(" " + personId + " " +name + " " + deptCode+ " " + parentDept + " "+deptName);
System.out.println();
}
}
My department class
public class department {
private int deptCode;
private String parentDept;
private String deptName;
public department(int deptCode, String parentDept, String deptName) {
super();
this.deptCode = deptCode;
this.parentDept = parentDept;
this.deptName = deptName;
}
public void dispalyDepartment() {
System.out.println(" "+deptCode+" "+parentDept+" "+deptName);
}
}
Now My goal is personDetails and departmentDetailsinto arraylist and compare and then findout the diference one bsed on dept code.
Here is my Logic : which is working fine.
List<person> listC = new ArrayList<person>();
for(person p : list) {
boolean flag = false;
for (department d:depList) {
if(p.deptCode == d.deptCode) {
flag = false;
break;
}else {
flag = true;
}
}
if(flag == true) {
listC.add(p);
}
}
o/p should be like
(6,"Horward",103,"Leadership","Managmnet");
(7,"Cyna",104,"HR","Human Resource");
because deptCode : 103 and 104 is not present.
Can anybody help me here. Can I use any other collection technique.