如何比较java中的两个hashmaps? [英] How to compare two hashmaps in java?

查看:73
本文介绍了如何比较java中的两个hashmaps?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个散列贴图,如下所示:

  1。============ =员工================= 


关键:1_10:价值:13/04/2012
关键:1_11:价值: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:价值: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。=============工作日============= ====

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

键不一样,但值是相同的,有时价值不同。



我想找出 employeemap workingdaymap


$ b

  if(!employeeMap.containsValue(workingdayMap.get(wKey))){
// tak e在workingdayMap中存在的employeeMap中不存在的值,但是它在workingdayMap
}

中存在

您可以帮我解决这个问题吗?



问候
Tony

解决方案

下面是代码完美的作用,比较失败的原因是map值不同。

 关键:27:价值:27/4/2012,关键:1_18:价值:27/04/2012 

如果我们看到日期,则可以清楚地看到差异。 2012年4月27日 2012年4月27日不同,因为我将它存储为一个字符串。

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

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

感谢您们的支持和对投票结果负责的人。

:)
Tony


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

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.

Could you please help me to solve this?

Regards Tony

解决方案

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.

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

这篇关于如何比较java中的两个hashmaps?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆