I have two hashmap HashMap<String,List<String>> in this format how to compare both the values are same or not with same keys

huangapple 未分类评论46阅读模式
英文:

I have two hashmap HashMap<String,List<String>> in this format how to compare both the values are same or not with same keys

问题

Sure, here's the translated content:

HashMap<String, List<String>> dPlatemap

HashMap<String, List<String>> ePlatemap

这个映射包含值的列表
英文:
HashMap&lt;String,List&lt;String&gt;&gt; dPlatemap

HashMap&lt;String,List&lt;String&gt;&gt; ePlatemap

The map contains List of values

答案1

得分: 0

你的问题 "如何比较两个具有相同键的值是否相同" 可能需要一些澄清:

  1. 如果您想要检查两个映射是否具有相同的键,并且由于它是一个列表,每个键具有相同的值和顺序,您可以使用
    dPlatemap.equals(ePlatemap)

  2. 如果您想要检查两个映射是否具有相同的值,对于给定的键,可以添加一个小的实用方法,类似于这样:

boolean checkEqual(Map<String, List<String>> dPlatemap, Map<String, List<String>> ePlatemap, String key) {
    if (!dPlatemap.containsKey(key) && !ePlatemap.containsKey(key)) 
        return true;
    if (!dPlatemap.containsKey(key) || !ePlatemap.containsKey(key)) 
        return false;
    return dPlatemap.get(key).equals(ePlatemap.get(key));
}
英文:

Your question "how to compare both the values are same or not with same keys" may need some clarification:

  1. If you want to check if both maps have the same keys, and each key has same values and order since it's an List, you can use
    dPlatemap.equals(ePlatemap)

  2. If you want to check if both maps have the same values, for a given key, add a small util method could help, sth. like that:

boolean checkEqual(Map&lt;String, List&lt;String&gt;&gt; dPlatemap, Map&lt;String, List&lt;String&gt;&gt; ePlatemap, String Key) {
    if (!dPlatemap.containsKey(key) &amp;&amp; !ePlatemap.containsKey(key)) 
        return true;
    if (!dPlatemap.containsKey(key) || !ePlatemap.containsKey(key)) 
        return false;
    return  dPlatemap.get(key).equals(ePlatemap.get(key));
}

huangapple
  • 本文由 发表于 2020年7月26日 05:49:08
  • 转载请务必保留本文链接:https://java.coder-hub.com/63093900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定