使用equals和hashCode方法比较对象的动态字段

时间:2013-06-26 13:45:44

标签: java search object collections

比较同一类的不同对象及其内容,如jobTitleId,classificationId,deptId& categoryId将要完成并稍后使用Set和Map进行一些操作。我能够通过简单地覆盖Object类的equals和hashCode方法来实现这一点,并且能够获取信息(如下面的Map中所示)。

Map<LocationData, List<LocationData>>

以下是我使用过的类(它已经显示给你,以便可以参考我的问题陈述):

LocationData类

package com.astreait.bulkloader;

public class LocationData {     
    String locId, deptId, jobTitleId, classificationId;

    @Override   
    public boolean equals(Object obj) {         
        LocationData ld = (LocationData)obj;        
        return this.deptId.equals(ld.deptId) && this.jobTitleId.equals(ld.jobTitleId) && this.classificationId.equals(ld.classificationId) &&
this.locId.equals(ld.locId);    
    }

    @Override   
    public int hashCode() {         
        return deptId.hashCode() + jobTitleId.hashCode() + classificationId.hashCode() +locId.hashCode();   
    }
}

问题:

我已经知道这个对象的所有字段都需要进行比较。 即我必须使用名为classificationId,deptId,jobTitleId&amp;的变量。 locId等。

需要: 我需要自定义这个逻辑,以便可以动态地提取字段Names(classificationId,deptId,jobTitleId&amp; locId等)及其值。所以,据我所知,我使用了2个类(TableClass和ColWithData),因此TableWlass对象中存在ColWithData列表。

我在想如果我覆盖相同的两种方法equals() & hashCode(); 这样就可以实现同样的目标。

TableClass class #1

class TableClass{
    List<ColWithData> cwdList;

    @Override
    public boolean equals(Object obj) {
        boolean returnVal = false;
                // I need to have the logic to be defined such that 
                // all of the dynamic fields can be compared
        return returnVal;
    }

    @Override
    public int hashCode() {
        int returnVal = 0;
                // I need to have the logic to be defined such that 
                // all of the dynamic fields can be found for their individual hashCodes
        return returnVal;
    }
}

ColWithData类#2

class ColWithData{ 

    String col; // here the jobTitleId, classificationId, deptId, locId or any other more fields info can come.
    String data; // The corresponding data or value for each jobTitleId, classificationId, deptId, locId or any other more fields.

}

如果我正在朝着正确的方向前进,或者我应该采取任何其他方法,请告诉我。如果可以使用当前的方法,那么应该在equals和hashCode方法中执行什么?

最后我需要将地图制作成:(它不是我将如何制作,但可以被认为是我希望的结果来自这个逻辑)

Map<TableClass, List<TableClass>> finalMap;

编辑我被投了票。所以,我再次对我的要求做了一些修改。 (请帮我解决这个问题)

2 个答案:

答案 0 :(得分:1)

使用此类ColWithData有点难看。您应该使用Map<String,String>

package mypack;

import java.util.*;

public class TableClass {
    /* HashMap containing your values:
       map.put("locId", [data]);
       ...
    */
    public Map<String,String> cwdMap;

    public Map<String,String> getCwdMap() {
        return cwdMap;
    }

    public void setCwdMap(Map<String,String> cwdMap) {
        this.cwdMap = cwdMap;
    }

    @Override
    public boolean equals(Object obj) {
        TableClass tClass = (TableClass) obj;

        for(String col: this.cwdMap.keyset()){
            if (! tClass.cwdMap.get(col).equals(this.cwdMap.get(col)){
                return false;
            }
        }

        return true;
    }

    @Override
    public int hashCode() {
        int hCode = 0;
        for(String col: this.cwdMap.keyset()){
            hCode = hCode+cwdMap.get(col).hashCode();
        }

        return hCode;
    }
}

在此代码中,我从不检查null值,但您可能应该检查。

还有一件事让我感到困惑:

如果您的媒体资源(cwdList)为public,为什么要使用getter / setter?

答案 1 :(得分:0)

我想我找到了解决方案并且它为我工作。 如果可以找到解决此问题的简单方法或其他方法,请告诉我。

代码段是:

package mypack;

import java.util.*;

public class TableClass {
    public List<ColWithData> cwdList;

    public List<ColWithData> getCwdList() {
        return cwdList;
    }

    public void setCwdList(List<ColWithData> cwdList) {
        this.cwdList = cwdList;
    }

    @Override
    public boolean equals(Object obj) {

        TableClass tClass = (TableClass) obj;

        boolean returnVal = true;



        for(ColWithData cwd: this.getCwdList()){
            for(ColWithData innerCwd: tClass.getCwdList()){
                if(cwd.getCol().equalsIgnoreCase(innerCwd.getCol())){
                    if(!cwd.getData().equalsIgnoreCase(innerCwd.getData()))
                        returnVal = false;
                }
            }
        }

        return returnVal;
    }

    @Override
    public int hashCode() {
        int hCode = 0;
        for(ColWithData cwd: this.getCwdList()){
            hCode = hCode+cwd.getData().hashCode();
        }

        return hCode;
    }

}

最后做了一张地图说:

Map<TableClass, List<TableClass>> map = new LinkedHashMap<TableClass, List<TableClass>>();

根据需要显示内容。

相关问题