英文:
Find object in ArrayList
问题
我想在ArrayList
中找到一个LegalEntity
对象。该对象可能是不同的实例。我只关心它们是否代表相同的值,即它们具有相同的主键。所有LegalEntity
实例都是由EJB从数据库值创建的:
List<LegalEntity> allLegalEntities = myEJB.getLegalEntityfindAll();
LegalEntity currentLegalEntity = myEJB.getLegalEntityfindById(123L);
我的第一个天真的想法从未找到匹配项:
if (allLegalEntities.contains(currentLegalEntity)) {
}
然后我想也许我需要创建自己的equals()
方法:
public boolean equals(LegalEntity other) {
return legalEntityId.equals(other.legalEntityId);
}
但是这个方法甚至没有被调用。有没有一种方法可以找到一个列表中的对象,而不涉及循环?
我正在学习Java,所以这可能很容易是我的一些愚蠢误解。
英文:
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.
答案1
得分: 2
你的方法是正确的,但你需要覆盖接受一个 Object
参数的 equals 方法:
@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;
// 根据一些属性检查是否相等
}
然而,你还需要覆盖 hashCode
方法:
@Override
public int hashCode() {
// 返回一个唯一的整数
}
因此,这可能不是最简单的解决方案。
另一种方法是使用 filter
:
LegalEntity myLegalEntity = myEJB.getLegalEntityfindAll().stream()
.filter(legalEntity -> legalEntity.getProperty().equals("something"))
.findAny()
.orElse(null);
更多信息在这里
英文:
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
答案2
得分: 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();
英文:
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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论