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