【问题标题】:Can we compare two java collections with dynamic equals method?我们可以用动态equals方法比较两个java集合吗?
【发布时间】:2014-04-08 09:05:18
【问题描述】:

假设我们有一个“Client”对象:

(我只是在下面提到'Client'对象的属性和equals方法!!)

public class Client {
    private Long clientId;
    private String clientName;
    private Integer status;

    //getters and setters for above attributes
    .....
    ...

    //hashCode method
    ....
    ..

    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Client other = (Client) obj;
        if (clientId == null) {
            if (other.clientId != null)
                return false;
        } else if (!clientId.equals(other.clientId))
            return false;
        if (clientName == null) {
            if (other.clientName != null)
                return false;
        } else if (!clientName.equals(other.clientName))
            return false;
        if (status == null) {
            if (other.status != null)
                return false;
        } else if (!status.equals(other.status))
            return false;
        return true;
    }
}

从上面的 equals 方法可以清楚地看到,如果两个对象的所有属性都相同,则说'两个'客户端对象是相等的。

现在假设我需要比较客户端对象的两个集合(命名为incomingClients 和existingClients)。
从 csv/xls 文件中读取“客户端”数据后,生成了第一个集合(集合传入客户端)。
第二个集合(Collection existingClients)包含系统中当前存在的所有客户端。

我可以执行以下代码(使用 apache CollectionUtils)来获取“通用”客户端。

Collection<Client> commonClients = (Collection<Client>)CollectionUtils.intersection(incomingClients,existingClients);

现在使用下面的代码,我可以从两个集合中删除这些 commonClients。

incomingClients.removeAll(commonClients);
existingClients.removeAll(commonClients);

删除“公共客户对象”的目的是,我们不需要对这些记录进行“任何处理”, 因为我们真的对那些记录一点都不感兴趣。

现在我如何才能确定“Collection incomingClients”集合中哪些是完全“新客户”? (当我说“新”时,它意味着一个客户有一个新的“clientId”,它在“Collection existingClients”中不存在)

另外,我如何确定哪些客户需要“修改” (当我说“修改”时,它意味着“集合传入客户端”和集合现有客户端“ 具有相同的 clientId,但不同的“clientName”)

我知道我们可以执行正常的“for”循环(“检查下方”)来找出“新”/“需要修改”的客户端。

但我想写“一些新的东西”,我们是否可以使用“Apache CollectionUtils”包中的一些类/函数来实现这一点。

Collection<Client> newClients = new ArrayList<Client>();
Collection<Client> toBeModifiedClients = new ArrayList<Client>();
boolean foundClient = false;
Client client = null;
for(Client incomingClient :incomingClients){
    foundClient = false;
    for(Client existingClient : existingClients){
    if(existingClient.getClientId().equals(incomingClient.getClientId())){
        client = existingClient;
        foundClient = true;
        break;
    }
   }
   if(foundClient){
        toBeModifiedClients.add(client);
    }else{
        //not found in existing. so this is completely new
        newClients.add(incomingClient);
    }
}

我是在“复杂化”一个简单的东西吗? 有什么想法吗??

【问题讨论】:

    标签: collections apache-commons


    【解决方案1】:

    首先,是的,你把“简单的东西”复杂化了。您的整个问题可以总结如下:

    给定集合 A 和 B,我如何使用 CollectionUtils 获得以下信息:

    1. A-B,使用确定相等性的特定函数
    2. A∩B,使用确定相等性的特定函数

    所以,是的。 CollectionUtils 有你需要的。看CollectionUtils.select()

    【讨论】:

      猜你喜欢
      • 2013-02-12
      • 2011-04-26
      • 1970-01-01
      • 2019-10-09
      • 1970-01-01
      • 1970-01-01
      • 2012-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多