javajakarta-eecomparatorcomparetoobject-comparison

Compare the object with a list of contents to another object


I have an object with a list of contents in it and the other object with contents in it without the list like the code below

List<LoginInformation> allUsersLoginInfo
LoginInformation loginInformation

Now I want to compare these two and see if the elements of loginInformation exists in allUsersLoginInfo

LoginInformation is a model class.

It has Name and Rollnumber.

So, allUserLoginInfo is a list which contains multiple values for Name and Rollnumber.

Now, I want to compare and see if any value of loginInformation (i.e.,either the value of Name or RollNumber) presents in allUserLoginInfo then gives me true else false noting that no values are equal.

Thanks in advance


Solution

  • You can use Stream#anyMatch to accomplish the task at hand.

    if(allUsersLoginInfo.stream().anyMatch(m -> m.getName().equals(loginInformation.getName())
                       || m.getRollNumber() == loginInformation.getRollNumber())){
            //exists
    }else{
          // does not exist
    }