Java 比较对象的地图
我已经设置了一个测试:Java 比较对象的地图,java,collections,map,Java,Collections,Map,我已经设置了一个测试: 检索有关多个法庭案例的数据:每个法庭案例存储在一个法庭案例对象中 然后将一组法庭对象存储在地图中 我检索了两次这些数据(从两个不同的来源),因此最终得到了两张地图 对象内的数据在地图之间应相同,但地图内对象的顺序可能不同: 地图1: A、 案例1-B,案例2-C,案例3 地图2: B、 案例2-A,案例1-C,案例3 如何才能最好地比较这两个映射?映射实现提供了一个equals方法来实现这一点 Map实现提供了一个equals方法来实现这一点 Map#equals不关
- 检索有关多个法庭案例的数据:每个法庭案例存储在一个法庭案例对象中
- 然后将一组法庭对象存储在地图中
- 我检索了两次这些数据(从两个不同的来源),因此最终得到了两张地图
如何才能最好地比较这两个映射?映射实现提供了一个equals方法来实现这一点 Map实现提供了一个equals方法来实现这一点
Map#equals
不关心顺序。只要两个映射包含相同的映射,它就会返回true
Map#equals
使用Set#equals
方法,应用于条目集:
如果指定的对象也是一个集合,两个集合的大小相同,并且指定集合的每个成员都包含在此集合中(或者等效地,此集合的每个成员都包含在此指定集合中),则返回true
注意:这假设您的CourtCase
对象有适当的equals
和hashcode
方法进行比较。Map#equals
不关心顺序。只要两个映射包含相同的映射,它就会返回true
Map#equals
使用Set#equals
方法,应用于条目集:
如果指定的对象也是一个集合,两个集合的大小相同,并且指定集合的每个成员都包含在此集合中(或者等效地,此集合的每个成员都包含在此指定集合中),则返回true
注意:这假设您的
CourtCase
对象有适当的equals
和hashcode
方法进行比较。@user973718在java中比较两个map对象的最佳方法是-您可以将map的键添加到列表中,并在这两个列表中使用方法retainal()和removeAll()并将它们添加到另一个常用键列表和不同的键列表中。使用公共列表和不同列表的键,您可以在映射中迭代,使用equals可以比较映射
下面的代码给出了此输出:
在{b=2,c=3,a=1}之前
在{c=333,a=1}之后
不相等:在-333之前-3之后
相等:在-1之前在-1之后
仅在映射之前显示的值:2
import java.util.ArrayList;
导入java.util.HashMap;
导入java.util.Iterator;
导入java.util.List;
导入java.util.Map;
导入org.apache.commons.collections.CollectionUtils;
公开课演示
{
公共静态void main(字符串[]args)
{
Map beforeMap=新建HashMap();
放在地图之前(“a”、“1”);
放在地图之前(“b”、“2”);
放在地图之前(“c”、“3”);
Map afterMap=newhashmap();
后置地图。放置(“a”、“1”);
后置地图放置(“c”、“333”);
System.out.println(“Before”+beforeMap);
System.out.println(“在”+后地图之后);
List beforeList=getAllKeys(beforeMap);
List afterList=getAllKeys(afterMap);
列表commonList1=在列表之前;
列表commonList2=后列表;
List diffList1=getAllKeys(beforeMap);
List diffList2=getAllKeys(afterMap);
公共列表1.保留(后列表);
公共列表2.保留(在列表之前);
diffList1.removeAll(commonList1);
diffList2.removeAll(commonList2);
if(commonList1!=null&commonList2!=null)//尽管两个大小相同
{
对于(int i=0;i
@user973718在java中比较两个映射对象的最佳方法是-您可以将映射的键添加到列表中,使用这两个列表,您可以使用方法retainal()和removeAll()将它们添加到另一个公共键列表和不同的键列表中。使用公共列表和不同列表的键,您可以在映射中迭代,使用equals可以比较映射
下面的代码给出了此输出:
在{b=2,c=3,a=1}之前
在{c=333,a=1}之后
不相等:在-333之前-3之后
相等:在-1之前在-1之后
仅在映射之前显示的值:2
import java.util.ArrayList;
导入java.util.HashMap;
导入java.util.Iterator;
导入java.util.List;
导入java.util.Map;
导入org.apache.commons.collections.CollectionUtils;
公开课演示
{
公共静态void main(字符串[]args)
{
Map beforeMap=新HashMap
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
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);
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;
}
}