我的应用程序存储从数据库和用户输入的值,并进行比较。为了存储这些值,我使用了哈希图,其中键为整数,数据为列表,并存储值。键值和数据不一定相同,但是两个哈希图中的所有数据值都应该相同。
哈希图如下: 键: 值
1-[cd,done]
2-[mb,done]
3-[core,done]
另一个是:
1-[mb,done]
2-[core,done]
3-[cd,done]
在代码中,无论值以什么顺序存储,上面的哈希映射应该是相等的,但目前我无法正确编码它...
我的应用程序存储从数据库和用户输入的值,并进行比较。为了存储这些值,我使用了哈希图,其中键为整数,数据为列表,并存储值。键值和数据不一定相同,但是两个哈希图中的所有数据值都应该相同。
哈希图如下: 键: 值
1-[cd,done]
2-[mb,done]
3-[core,done]
1-[mb,done]
2-[core,done]
3-[cd,done]
在代码中,无论值以什么顺序存储,上面的哈希映射应该是相等的,但目前我无法正确编码它...
containsAll
方法比较 values()
:hashMap1.values().containsAll(hashMap2.values()) &&
hashMap2.values().containsAll(hashMap1.values());
您还可以使用CollectionUtils.isEqualCollection来简化代码:
CollectionUtils.isEqualCollection(hashMap1.values(), hashMap2.values());
public static void main(String[] args) {
公共静态无返回值主函数(String[] args){ Map<Integer,String> map1 = new HashMap<Integer,String>();
Map<Integer,String> map2 = new HashMap<Integer,String>();
map1.put(1, "ABC");
map1.put(2, "1123");
map2.put(1, "XYZ");
boolean val = map1.keySet().containsAll(map2.keySet());
for (Entry<Integer, String> str : map1.entrySet()) {
System.out.println("================="+str.getKey());
if(map2.containsKey(str.getKey())){
System.out.println("Map2 Contains Map 1 Key");
}
}
System.out.println("================="+val);
}