简体   繁体   English

Java中2个对象列表之间的区别

[英]Difference between 2 lists of objects in Java

I have a obj type MyObj as follow: 我有一个obj类型MyObj ,如下所示:

  class MyObj{
    String id;
    String username;
    String fullName;
    String age;

    //getters & setters
}

Suppose we have 2 lists containing different number of elements like so: 假设我们有2个包含不同数量元素的列表,如下所示:

List<MyObj> listA
List<MyObj> listB

I have a generic method that detects elements from listA that are missing in listB : 我有一个通用的方法来检测listA中缺少的listB

 public static <T> List<T> getListDifference(List<T> list1, List<T> list2) {
        Collection<T> first = new HashSet<T>(list1);
        Collection<T> second = new HashSet<T>(list2);
        first.removeAll(second);
        return new ArrayList<T>(first);
    }

If objects from listA and listB has the same fields for all items, everything works just fine. 如果listAlistB中的对象的所有项目都具有相同的字段,则一切正常。

The problem is that some items has only id and username but others can have fullName or age too, and as result this method doesn't work anymore. 问题在于某些项目仅具有idusername而另一些项目也可以具有fullNameage ,因此该方法不再起作用。 I'd like to keep the same logic, considering only id field because it's present in all objects. 我想保持相同的逻辑,只考虑id字段,因为它存在于所有对象中。

One obvious method is to copy only object's id field in other List<String> and work with obtained lists to detect elements, then just search for obj from both lists by id . 一种明显的方法是仅复制其他List<String>对象的id字段,并使用获取的列表来检测元素,然后仅通过id从两个列表中搜索obj。 This method has a big complexity, because of multiple iterations. 由于多次迭代,该方法具有很大的复杂性。 Is there a short way to achieve this? 有没有一个简短的方法来实现这一目标?

You have to implement equals and hashCode to work with sets. 您必须实现equalshashCode才能使用集合。 Sets needs a way of recognising if one object is equal to another, and default behaviour is not gonna work here. 集合需要一种方法来识别一个对象是否等于另一个对象,并且默认行为在这里不起作用。

By the way, I suggest using retainAll method on a list, check this out. 顺便说一句,我建议在列表上使用retainAll方法, 检查一下。

override MyObjs equals method: 覆盖MyObjs equals方法:

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    MyObj myObj = (MyObj) o;

    return (id != null ? !id.equals(myObj.id) : myObj.id != null);
}

IntelliJ/Android Studio have helpers for automatically generating equals and hashCode where you can choose the necessary properties. IntelliJ / Android Studio具有用于自动生成equalshashCode ,您可以在其中选择必要的属性。

As @ Kevin Esche mentioned, you have to implement equals() and hashCode() in your MyObj POJO, and according to your situation, they should be: 如@ Kevin Esche所述,您必须在MyObj POJO中实现equals()hashCode() ,根据您的情况,它们应为:

   @Override
public int hashCode() {
    return Objects.hash(id);
}

@Override
public boolean equals(Object obj) {
    if (obj == this) return true;
    if (!(obj instanceof MyObj)) return false;

    MyObj myObj= (MyObj) obj;
    return Objects.equals(id, myObj.id);
}

That will work even if ages are different and the ids are the same for example. 即使年龄不同且ID相同,这也将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM