在ArrayList中查找对象

huangapple go评论120阅读模式
英文:

Find object in ArrayList

问题

我想在ArrayList中找到一个LegalEntity对象。该对象可能是不同的实例。我只关心它们是否代表相同的值,即它们具有相同的主键。所有LegalEntity实例都是由EJB从数据库值创建的:

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

我的第一个天真的想法从未找到匹配项:

  1. if (allLegalEntities.contains(currentLegalEntity)) {
  2. }

然后我想也许我需要创建自己的equals()方法:

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

但是这个方法甚至没有被调用。有没有一种方法可以找到一个列表中的对象,而不涉及循环?

我正在学习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:

  1. List&lt;LegalEntity&gt; allLegalEntities = myEJB.getLegalEntityfindAll());
  2. LegalEntity currentLegalEntity = myEJB.getLegalEntityfindById(123L);

My first naive idea never finds matches:

  1. if (allLegalEntities.contains(currentLegalEntity)) {
  2. }

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

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

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 方法:

  1. @Override
  2. public boolean equals(Object obj) {
  3. if (this == obj)
  4. return true;
  5. if (obj == null)
  6. return false;
  7. if (getClass() != obj.getClass())
  8. return false;
  9. LegalEntity other = (LegalEntity) obj;
  10. // 根据一些属性检查是否相等
  11. }

然而,你还需要覆盖 hashCode 方法:

  1. @Override
  2. public int hashCode() {
  3. // 返回一个唯一的整数
  4. }

因此,这可能不是最简单的解决方案。

另一种方法是使用 filter

  1. LegalEntity myLegalEntity = myEJB.getLegalEntityfindAll().stream()
  2. .filter(legalEntity -> legalEntity.getProperty().equals("something"))
  3. .findAny()
  4. .orElse(null);

更多信息在这里

英文:

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

  1. @Override
  2. public boolean equals(Object obj) {
  3. if (this == obj)
  4. return true;
  5. if (obj == null)
  6. return false;
  7. if (getClass() != obj.getClass())
  8. return false;
  9. LegalEntity other = (LegalEntity) obj;
  10. // check if equals based one some properties
  11. }

However you also need to override hashCode:

  1. @Override
  2. public int hashCode() {
  3. // return a unique int
  4. }

So this might not be the easiest solution.

Another approach is to use filter:

  1. LegalEntity myLegalEntity = myEJB.getLegalEntityfindAll().stream()
  2. .filter(legalEntity -&gt; legalEntity.getProperty().equals(&quot;someting&quot;))
  3. .findAny()
  4. .orElse(null);

More info here

答案2

得分: 1

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

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

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

  1. List&lt;LegalEntity&gt; allLegalEntities = myEJB.getLegalEntityfindAll());
  2. LegalEntity currentLegalEntity = allLegalEntities.stream().filter(entity -&gt; entity.getId() == 123L).findFirst();

huangapple
  • 本文由 发表于 2020年7月28日 17:18:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63130871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定