3

I want to find a LegalEntity object in an ArrayList. The object can possibly be a different instance. I'm only interested in whether they represent the same value, i.e. they have the same primary key. All LegalEntity instances are created from database values by EJB:

List<LegalEntity> allLegalEntities = myEJB.getLegalEntityfindAll());
LegalEntity currentLegalEntity = myEJB.getLegalEntityfindById(123L);

My first naive idea never finds matches:

if (allLegalEntities.contains(currentLegalEntity)) {
}

I then thought that perhaps I need to create my own equals() method:

public boolean equals(LegalEntity other) {
    return legalEntityId.equals(other.legalEntityId);
}

But this method is not even being invoked. Is there a way to find an object in a list that doesn't involve looping?

I'm learning Java so it might easily be some foolish misunderstanding on my side.

8
  • 4
    you need to override the equals method, not overload it Commented Jul 28, 2020 at 9:21
  • You are correct with the equals method, but since you changed the method signature, your own equals method is not the method that is being called. You need to override public boolean equals(Object other) and define there what equality means in that case (you mentioned same primary key). Commented Jul 28, 2020 at 9:23
  • While contains() does use equals(Object), your equals(LegalEntity) is not equals(Object). Even so, overriding equals(Object) may not be the correct option, as equality is a difficult concept. You could just write a helper method with a for-loop in it to find it. You didn't really take the easy road in learning Java from EJB and Java 6. Commented Jul 28, 2020 at 9:23
  • With public boolean equals(Object other) it appears to work (Object doesn't have a legalEntityId property but I can force a cast inside the method). What edge cases can possibly bite my foot in the future? Commented Jul 28, 2020 at 9:33
  • @ÁlvaroGonzález with that method, it won't work. You need to pass an object, in the equals method, check if it's an instance of LegalEntity, if not, return false, if so, cast Commented Jul 28, 2020 at 9:35

2 Answers 2

2

Your approach is correct, but you need to override the method equals that accepts an Object:

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    LegalEntity other = (LegalEntity) obj;
    // check if equals based one some properties
}

However you also need to override hashCode:

@Override
public int hashCode() {
    // return a unique int
}

So this might not be the easiest solution.

Another approach is to use filter:

LegalEntity myLegalEntity = myEJB.getLegalEntityfindAll().stream()
                              .filter(legalEntity -> legalEntity.getProperty().equals("someting"))
                              .findAny()
                              .orElse(null);

More info here

Sign up to request clarification or add additional context in comments.

Comments

1

If you're using Java 8 you can use streams:

List<LegalEntity> allLegalEntities = myEJB.getLegalEntityfindAll());
LegalEntity currentLegalEntity = allLegalEntities.stream().filter(entity -> entity.getId() == 123L).findFirst();

Comments

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.