Java比较两个映射

Java比较两个映射,java,Java,在java中,我想比较两个映射,如下所示,我们是否有现有的API来实现这一点 谢谢 Map<String, String> beforeMap ; beforeMap.put("a", "1"); beforeMap.put("b", "2"); beforeMap.put("c", "3"); Map<String, String> afterMap ; afterMap.put("a", "1"); afterMap.put("c", "333"); //---

在java中,我想比较两个映射,如下所示,我们是否有现有的API来实现这一点

谢谢

Map<String, String> beforeMap ;
beforeMap.put("a", "1");
beforeMap.put("b", "2");
beforeMap.put("c", "3");

Map<String, String> afterMap ;
afterMap.put("a", "1");
afterMap.put("c", "333");

//--- it should give me:
b is missing, c value changed from '3' to '333'
Map-beforeMap;
放在地图之前(“a”、“1”);
放在地图之前(“b”、“2”);
放在地图之前(“c”、“3”);
后地图;
后置地图。放置(“a”、“1”);
后置地图放置(“c”、“333”);
//---它应该给我:
缺少b,c值从“3”更改为“333”

没有现成的组件可以帮助您实现这一点。不幸的是,您可能必须对其进行编码。好消息是逻辑很简单。

< P>根据你的特殊需要,你也可以考虑使用其他设计的应用程序来做这项工作,比如DIFO。你可以将这两个映射写到两个不同的文件,并对文件进行区分。

< P> GuAVA类有一些方法来解决一对映射之间的差异。但是,这些方法为您提供了一个表示差异的数据结构,而不是一个漂亮的打印字符串。

您可以使用一个包含键和值的自定义对象(实际上Map在内部执行此操作,对用户隐藏,因此我们不能使用它)

将这些元组放入
集合

要比较两个集合,请将它们都转换为数组,对数组进行排序,并并行地从头到尾遍历两个数组,如果第一个数组的键小于第二个数组中的键,则将其向下移动,反之亦然

class Tuple implements Comparable<Tuple>
{
    public String   key;
    public String   value;

    public Tuple(String key, String value)
    {
        this.key = key;
        this.value = value;
    }

    @Override
    public int compareTo(Tuple o)
    {
        return key.compareTo(o.key);
    }
}

public static void main(String[] args)
{
    // TreeSet is already sorted. If you use HashSet, use Arrays.sort()
    Set<Tuple> beforeSet = new TreeSet<>();
    beforeSet.add(new Tuple("a", "1"));
    beforeSet.add(new Tuple("b", "2"));
    beforeSet.add(new Tuple("c", "4"));

    Set<Tuple> afterSet = new TreeSet<>();
    afterSet.add(new Tuple("a", "1"));
    afterSet.add(new Tuple("c", "333"));
    afterSet.add(new Tuple("aa", "4"));

    Tuple[] beforeArray = beforeSet.toArray(new Tuple[beforeSet.size()]);
    Tuple[] afterArray = afterSet.toArray(new Tuple[afterSet.size()]);

    int beforePtr = 0;
    int afterPtr = 0;
    while (beforePtr < beforeArray.length || afterPtr < afterArray.length)
    {
        int difference = afterPtr >= afterArray.length? -1 : beforePtr >= beforeArray.length? 1 : beforeArray[beforePtr].compareTo(afterArray[afterPtr]);
        if (difference == 0)
        {
            if (!beforeArray[beforePtr].value.equals(afterArray[afterPtr].value))
            {
                System.out.println(beforeArray[beforePtr].key + " value changed from '" + beforeArray[beforePtr].value + "' to '" + afterArray[afterPtr].value + "'");
            }
            beforePtr++;
            afterPtr++;
        }
        else if (difference < 0)
        {
            System.out.println(beforeArray[beforePtr].key + " is missing");
            beforePtr++;
        }
        else
        {
            System.out.println(afterArray[afterPtr].key + " is added");
            afterPtr++;
        }
    }
}
类元组实现了可比较的
{
公共字符串密钥;
公共字符串值;
公共元组(字符串键、字符串值)
{
this.key=key;
这个值=值;
}
@凌驾
公共整数比较(元组o)
{
返回键。比较键(o键);
}
}
公共静态void main(字符串[]args)
{
//TreeSet已排序。如果使用HashSet,请使用Arrays.sort()
Set beforeSet=新树集();
添加(新元组(“a”,“1”));
添加(新元组(“b”,“2”));
添加(新元组(“c”,“4”));
Set afterSet=新树集();
添加(新元组(“a”,“1”));
添加(新元组(“c”,“333”));
添加(新元组(“aa”,“4”));
元组[]beforeSet.toArray=beforeSet.toArray(新元组[beforeSet.size()]);
Tuple[]afterArray=afterSet.toArray(新的Tuple[afterSet.size());
int beforePtr=0;
int afterPtr=0;
while(beforePtr=afterArray.length?-1:beforePtr>=beforeArray.length?1:beforeArray[beforePtr]。比较(afterArray[afterPtr]);
如果(差==0)
{
如果(!beforeArray[beforePtr].value.equals(afterArray[afterPtr].value))
{
System.out.println(beforeArray[beforePtr].key+“值已从”“+beforeArray[beforePtr].value+“'更改为”“+afterArray[afterPtr].value+””;
}
beforePtr++;
afterPtr++;
}
否则如果(差异<0)
{
System.out.println(beforeArray[beforePtr].key+“丢失”);
beforePtr++;
}
其他的
{
System.out.println(添加了afterArray[afterPtr].key+);
afterPtr++;
}
}
}
我会使用Set to do Set differences of key的removeAll()功能来查找添加和删除。实际更改可以通过使用条目集作为HashMap进行设置差异来检测。条目使用键和值实现equals()

Set<String> removedKeys = new HashSet<String>(beforeMap.keySet());
removedKeys.removeAll(afterMap.keySet());

Set<String> addedKeys = new HashSet<String>(afterMap.keySet());
addedKeys.removeAll(beforeMap.keySet());

Set<Entry<String, String>> changedEntries = new HashSet<Entry<String, String>>(
        afterMap.entrySet());
changedEntries.removeAll(beforeMap.entrySet());

System.out.println("added " + addedKeys);
System.out.println("removed " + removedKeys);
System.out.println("changed " + changedEntries);

@用户595234要比较这两个映射,可以将映射的键添加到列表中,使用这两个列表,可以使用方法retainal()和removeAll()将它们添加到另一个公共键列表和不同的键列表中。使用公共列表和不同列表的键,您可以在映射中迭代,使用equals可以比较映射

    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;
            }
    }
公共类演示
{
公共静态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);
System.out.println(“映射前的公共列表”+公共列表1);
System.out.println(“映射后的公共列表”+commonList2);
System.out.println(“映射前的差异列表”+diffList1);
System.out.println(“映射后的差异列表”+diffList2);
if(commonList1!=null&commonList2!=null)//尽管两个大小相同
{
对于(int i=0;iString output = new String();
for (String key:beforeMap.getKeys()){
  String beforeValue = beforeMap.getValue(key);
  String afterValue = afterMap.getValue(key);
  //nullsafe
  if(beforeValue.equals(afterValue){}
  else if (afterValue == null){
      output = output + key + " is missing, ";
      continue;
  }else {
      output = output + key + " has changed from " + beforeValue + " to " + afterValue + " , ";
  }
  afterMap.remove(key);

}

for (String key:afterMap.getKeys()){
    output = output + key + " was added with value " + afterMap.getValue(key) + ", ";
}

if(output == null){
    output = "Same map";
}
output = output.substring(0,output.length-2);
System.out.println(output);
    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;
            }
    }