简体   繁体   English

如何在Java中比较两个哈希图?

[英]How to compare two hashmaps in java?

I have two hash maps like the following: 我有两个像下面的哈希图:

1.=============Employee=================


Key : 1_10 : Value : 13/04/2012
Key : 1_11 : Value : 18/04/2012
Key : 1_12 : Value : 19/04/2012
Key : 1_14 : Value : 23/04/2012
Key : 1_13 : Value : 20/04/2012
Key : 1_16 : Value : 25/04/2012
Key : 1_1  : Value : 02/04/2012
Key : 1_15 : Value : 24/04/2012
Key : 1_18 : Value : 27/04/2012
Key : 1_3  : Value : 04/04/2012
Key : 1_17 : Value : 26/04/2012
Key : 1_2  : Value : 03/04/2012
Key : 1_5  : Value : 06/04/2012
Key : 1_19 : Value : 30/04/2012
Key : 1_4  : Value : 05/04/2012
Key : 1_7  : Value : 10/04/2012
Key : 1_6  : Value : 09/04/2012
Key : 1_9  : Value : 12/04/2012
Key : 1_8  : Value : 11/04/2012

2.=============Working day=================

Key : 27 : Value : 27/4/2012
Key : 02 : Value : 02/4/2012
Key : 26 : Value : 26/4/2012
Key : 19 : Value : 19/4/2012
Key : 11 : Value : 11/4/2012
Key : 04 : Value : 04/4/2012
Key : 30 : Value : 30/4/2012
Key : 06 : Value : 06/4/2012
Key : 13 : Value : 13/4/2012
Key : 09 : Value : 09/4/2012
Key : 03 : Value : 03/4/2012
Key : 23 : Value : 23/4/2012
Key : 20 : Value : 20/4/2012
Key : 16 : Value : 16/4/2012
Key : 10 : Value : 10/4/2012
Key : 18 : Value : 18/4/2012
Key : 25 : Value : 25/4/2012
Key : 17 : Value : 17/4/2012
Key : 12 : Value : 12/4/2012
Key : 24 : Value : 24/4/2012
Key : 05 : Value : 05/4/2012

Keys are not same but the values are same and sometime the values are not same. 键不相同,但值相同,有时值也不相同。

i want to find out the value which are not present in employeemap when comparing with workingdaymap workingdaymap比较时,我想找出employeemap中不存在的值

if (!employeeMap.containsValue(workingdayMap.get(wKey))) {
    //take the value which is not present in employeeMap which is present in workingdayMap
}

but it gives me null from employeeMap. 但是它给我的employeeMap为空。

Could you please help me to solve this? 你能帮我解决这个问题吗?

Regards Tony 问候托尼

Are the Values Date objects? 值日期对象吗? If so you're attempting to see if the map contains the same object (well its hash, which will be different unless you stored the EXACT same instance in both maps) rather than the value of the object. 如果是这样,则您尝试查看映射是否包含相同的对象(以及它的哈希,除非您在两个映射中都存储了相同的实例,否则它的哈希将有所不同)而不是对象的值。

Something like this should work: 这样的事情应该起作用:

Set workingValues = new HashSet(workingdayMap.values());
workingValues.removeAll(employeeMap.values());

We get all the values out of workingdayMap using the values() method. 我们使用values()方法从workingdayMap获取所有值。 We don't want to modify this Collection directly as this will modify workingdayMap so we create a Set of our values called workingValues . 我们不希望修改此Collection直接,因为这将修改workingdayMap所以我们创建了一个Set我们称为价值workingValues

We then remove all the values found in employeeMap which will leave the values which are in workingdayMap but not in employeeMap in workingValues . 然后,我们删除在employeeMap中找到的所有值,这将保留在workingdayMap的值,而不是在workingValues中的employeeMap中的workingValues

Can you please try like this. 你能这样尝试吗?

    HashMap<String, Date> employeeMap = new HashMap<String, Date>();

    HashMap<String, Date> workingdaymap = new HashMap<String, Date>();

    Set<String> values1 = new HashSet<String>(employeeMap.values());
    Set<String> values2 = new HashSet<String>(workingdaymap.values());
    boolean equal = values1.equals(value2);

below is the code which works perfectly, the reason why the comparison failed is the map value is different . 下面的代码运行良好,比较失败的原因是映射值不同。

Key : 27 : Value : 27/4/2012, Key : 1_18 : Value : 27/04/2012

if we see the dates, the difference is clearly seen. 如果我们看到日期,则可以清楚地看到差异。 27/4/2012 is different from 27/04/2012 since i stored it as a string. 27/4/201227/04/2012不同,因为我将其存储为字符串。

Iterator<Map.Entry<String,String>> holyDayiterator = workingdayMap.entrySet().iterator();

                 while (holyDayiterator.hasNext()) {
                    Map.Entry holiDayEntry = holyDayiterator.next();
                    if(!employeeMap.containsValue((holiDayEntry.getValue()))){
                        System.out.println("works perfect");
                    }

Thanks guys for your support and people who have gave down vote also. 谢谢你们的支持以及也投下反对票的人们。

:) Tony :)托尼

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

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