英文:
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<String,List<String>> dPlatemap
HashMap<String,List<String>> ePlatemap
The map contains List of values
答案1
得分: 0
你的问题 "如何比较两个具有相同键的值是否相同" 可能需要一些澄清:
-
如果您想要检查两个映射是否具有相同的键,并且由于它是一个列表,每个键具有相同的值和顺序,您可以使用
dPlatemap.equals(ePlatemap)
-
如果您想要检查两个映射是否具有相同的值,对于给定的键,可以添加一个小的实用方法,类似于这样:
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:
-
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)
-
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<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));
}
专注分享java语言的经验与见解,让所有开发者获益!
评论