@ user595234 Để so sánh hai Maps, bạn có thể thêm các phím của một bản đồ để liệt kê và với những 2 danh sách mà bạn có thể sử dụng các phương pháp retainAll() và RemoveAll() và thêm chúng vào danh sách khóa phổ biến khác và danh sách các phím khác nhau. Sử dụng các phím của danh sách chung và danh sách khác nhau mà bạn có thể lặp qua bản đồ, bằng cách sử dụng bằng bạn có thể so sánh bản đồ.
public class Demo
{
public static void main(String[] args)
{
Map<String, String> beforeMap = new HashMap<String, String>();
beforeMap.put("a", "1");
beforeMap.put("b", "2");
beforeMap.put("c", "3");
Map<String, String> afterMap = new HashMap<String, String>();
afterMap.put("a", "1");
afterMap.put("c", "333");
System.out.println("Before "+beforeMap);
System.out.println("After "+afterMap);
List<String> beforeList = getAllKeys(beforeMap);
List<String> afterList = getAllKeys(afterMap);
List<String> commonList1 = beforeList;
List<String> commonList2 = afterList;
List<String> diffList1 = getAllKeys(beforeMap);
List<String> diffList2 = getAllKeys(afterMap);
commonList1.retainAll(afterList);
commonList2.retainAll(beforeList);
diffList1.removeAll(commonList1);
diffList2.removeAll(commonList2);
System.out.println("Common List of before map "+commonList1);
System.out.println("Common List of after map "+commonList2);
System.out.println("Diff List of before map "+diffList1);
System.out.println("Diff List of after map "+diffList2);
if(commonList1!=null & commonList2!=null) // athough both the size are same
{
for (int i = 0; i < commonList1.size(); i++)
{
if ((beforeMap.get(commonList1.get(i))).equals(afterMap.get(commonList1.get(i))))
{
System.out.println("Equal: Before- "+ beforeMap.get(commonList1.get(i))+" After- "+afterMap.get(commonList1.get(i)));
}
else
{
System.out.println("Unequal: Before- "+ beforeMap.get(commonList1.get(i))+" After- "+afterMap.get(commonList1.get(i)));
}
}
}
if (CollectionUtils.isNotEmpty(diffList1))
{
for (int i = 0; i < diffList1.size(); i++)
{
System.out.println("Values present only in before map: "+beforeMap.get(diffList1.get(i)));
}
}
if (CollectionUtils.isNotEmpty(diffList2))
{
for (int i = 0; i < diffList2.size(); i++)
{
System.out.println("Values present only in after map: "+afterMap.get(diffList2.get(i)));
}
}
}
/** getAllKeys API adds the keys of the map to a list */
private static List<String> getAllKeys(Map<String, String> map1)
{
List<String> key = new ArrayList<String>();
if (map1 != null)
{
Iterator<String> mapIterator = map1.keySet().iterator();
while (mapIterator.hasNext())
{
key.add(mapIterator.next());
}
}
return key;
}
}
Mã dưới đây sẽ cung cấp cho bạn kết quả này:
Trước: {b=2, c=3, a=1}
Sau: {c=333, a=1}
bất bình đẳng: Before- 3 dịch vụ hậu mãi 333
Equal: Before- 1 sau giờ 1
Giá trị chỉ hiện diện trước bản đồ: 2
Nguồn
2012-12-10 11:34:10
Tôi không nghĩ rằng có API ngoài hộp cho việc này. Bạn cần mã hóa logic. – kosa
bản sao có thể có của [cách so sánh hai bản đồ băm?] (Http://stackoverflow.com/questions/4591988/how-to-compare-two-hash-maps) –