0

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.

David
  • 4,266
  • 8
  • 34
  • 69
  • Your code working fine for me. Please check you put all info in question. – Eklavya Aug 07 '21 at 10:57
  • How can I find the difference between `personDetails` and `personDetails` based on `deptCode`. yes printing is working fine. but i need a difference therefore i am placin it into arraylst of object. – David Aug 07 '21 at 11:00
  • Ya I have added `person` and `department` class. now from here i wann add this array of object nto array list and then comparing two array list. Appericiate if any smarter way. – David Aug 07 '21 at 11:08
  • Did you try anything to compare the two arrays by dept code ? Please add your tried code which was not working. – Eklavya Aug 07 '21 at 11:15
  • No Actuay here first thing I am not sure weather i am able to get correct list or not, so I haven't write anything as of now for comapring the two array by dept code. please help me there. – David Aug 07 '21 at 11:17
  • @Eklavya By using this I have got the first and second list. https://www.javatpoint.com/java-list Can you please help me to get the diffence basedon value. I have upadted my main method also. – David Aug 07 '21 at 11:28
  • You can iterate person list using loop and check if dept exists in dept list using loop inside. If not then add in the new list. – Eklavya Aug 07 '21 at 11:34
  • Ya I am thinking so, can u please help me to write that. even u can write that as a answer. – David Aug 07 '21 at 11:36
  • Without showing your effort write answers with code directly is not right on SO. But your answer is similar to this https://stackoverflow.com/a/45171066/4207306 Hope it helps. – Eklavya Aug 07 '21 at 11:42
  • ya i do understand eklavya. I am writing the code for that. Previously i got stuck on arraylist only. now i have fixed that so i m working on ur idea. thanks much. – David Aug 07 '21 at 11:44
  • @Eklavya I have done with the solution and working fine as well. I have posted in question. Can you please have a look and do little code review if i have doing some extra stuf.. Thanks Much appriciate. – David Aug 07 '21 at 12:08

1 Answers1

1

Try this.

    Set<Integer> deptCodes = Arrays.stream(departmentDetails)
        .map(department -> department.deptCode)
        .collect(Collectors.toSet());
    List<person> persons = Arrays.stream(personDetails)
        .filter(person -> !deptCodes.contains(person.deptCode))
        .collect(Collectors.toList());
    persons.forEach(person -> person.display());

output:

 6 Horward 103 Leadership Managmnet

 7 Cyna 104 HR Human Resource
  • thanks for the answer. I have changed my main method. I am going to try now just wann knw from u that is that based on new code or old. Because I am thinking to store the delta from the person table in new list. – David Aug 07 '21 at 11:40
  • Thank you saka this works fine for me. can I store this into different list just for the curisty i am asking. – David Aug 07 '21 at 11:42