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.

11
  • Your code working fine for me. Please check you put all info in question. Commented Aug 7, 2021 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. Commented Aug 7, 2021 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. Commented Aug 7, 2021 at 11:08
  • Did you try anything to compare the two arrays by dept code ? Please add your tried code which was not working. Commented Aug 7, 2021 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. Commented Aug 7, 2021 at 11:17

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Thank you saka this works fine for me. can I store this into different list just for the curisty i am asking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.