两个map,循环一次取出key,value都相等的交集
Map<String, String> map1 = new HashMap<>();
map1.put("test1", "test");
map1.put("test2", "test2");
map1.put("test3", "test3");
map1.put("test4", "test4");
map1.put("test5", "test");
System.out.println("mp1:" + map1);
Map<String, String> map2 = new HashMap<>();
map2.put("test6", "test6");
map2.put("test1", "test7");
map2.put("test8", "test8");
map2.put("test4", "test");
map2.put("test5", "test");
map2.put("test9", "test1");
System.out.println("mp2:" + map2);
Map<String, String> map3 = new HashMap<>();
Iterator iteratorTemp = map1.entrySet().iterator();
while (iteratorTemp.hasNext()) {
Map.Entry entry = (Entry) iteratorTemp.next();
System.out.println(entry);
if (map2.containsKey(entry.getKey())
&& entry.getValue().equals(map2.get(entry.getKey()))) {
map3.put((String) entry.getKey(), (String) entry.getValue());
}
}
System.out.println("一次循环map1和map2的交集为:" + map3);
原文:http://www.cnblogs.com/xiaozi-autotestman/p/6536545.html