1

I have 2 lists:

  • List1: Object1 (Empno1, Empname1,Salary1)

  • List2: Object2(Empno2, Empname2,Salary2) Object3(Empno3, Empname3,Salary3)

Given the size of List1 is not same as List2.

I want to iterate over the List1 and List2, if the Empno in List1 object is same as Empno of List2 Object then we have to validate Empname in List1 object is same as Empname of List2 Object

here is the code using old java:

boolean flag=false;

  for(Object1 obj1:list1) {

    for(Object2 obj2:list2) {

      if(obj1.getEmpno()==obj2.getEmpno()) {

         if(obj1.getEmpname().equals(obj2. getEmpname())){

          flag=true;

      }

     }

   }

}

Which is the best way to implement this with java8

Naman
  • 27,789
  • 26
  • 218
  • 353
  • Actual naming convention used in the code would have been good to use to make the question much clear. What did you try further? – Naman Aug 30 '19 at 04:45
  • 1
    You must explore the possibility of overriding equals method, for this kind of requirements. – Kris Aug 30 '19 at 04:50
  • @Naman I want to make one flag true if all the conditions satisfies – Spandhana Vunnam Aug 30 '19 at 05:45
  • 1
    What is the relation between `Object1` and `Object2` ? Those have the same attribute but not the same class. – AxelH Aug 30 '19 at 06:20
  • I think what you are looking for is a "zip" method. See: https://stackoverflow.com/questions/31963297/how-to-zip-two-java-lists – Sweeper Aug 30 '19 at 06:21

2 Answers2

0

one possible solution to find if there is one exact object in one another list is:

boolean b = list1.parallelStream().anyMatch(s -> list2.contains(s));

as @Kris mentioned too, it needs the equal method to be implemented for your Object class to be overridden like it:

class Object1 {
    int empNo;
    String empName;

    @Override
    public boolean equals(Object obj) {
        return ((Object2)obj).empNo == this.empNo && 
                ((Object2)obj).empName.equalsIgnoreCase(this.empName);
    }

    @Override        
    public int hashCode() {
        int result = 17;
        result = 31 * result + Integer.valueOf(empNo).hashCode();
        result = 31 * result + this.empName.hashCode();
        return result;
    }
}

The 17 31 hash code idea is from Effective Java book and for further information about it, follow: https://stackoverflow.com/a/299748/2137378

  • You can use any method for comparing, it is not necessary to override equals and use contains – belbix Aug 30 '19 at 06:37
  • Please also override `hashCode`, else someone will copy this code and learn a bad lesson. – sfiss Aug 30 '19 at 07:32
  • 1
    this is so wrong: you'll get `ClassCastException` every time you call `equals` with any other parameter type than `Object2`. Also you are accessing `Object2` fields directly without getters – Adrian Aug 30 '19 at 07:57
  • @Adrian, yeah the method depends on these two types. But you are wrong. It does not mean if it is strongly dependent on a class, it can not be executed correctly for the purpose it has been implemented. The problem starts with the first step of designing such an architecture. Above code is not a whole problem solution. It is just a solution to a partial part of a program as the question requested. It's better to have a logical level of complexity when suggesting a solution to a question :) And also we are not writing code in a class to add a pure classic one for our students. – Amin Heydari Alashti Aug 30 '19 at 08:05
  • Instead of `Integer.valueOf(empNo).hashCode()` you can use `Integer.hash(empNo)`, though both constructs will just evaluate to `empNo`. But you can simply use `return Objects.hash(empNo, empName);` Besides that, a broken `equals` method is not “a solution to a partial part of a program”, it’s just broken. Especially, when nothing in the answer indicates that this code is incomplete. What’s so hard inserting the usual `if(obj == this) return true; if(!(obj instanceof Object2)) return false;` at the beginning? – Holger Aug 30 '19 at 08:47
0

just use Stream::anyMatch method

boolean result = list1.stream()
            .anyMatch(o1 -> list2.stream()
                    .anyMatch(o2 -> o1.getEmpno() == o2.getEmpno()
                            && o1.getEmpname().equals(o2.getEmpname())));
Adrian
  • 2,984
  • 15
  • 27