Java 比较两个集合以比较两个文本文件的添加、删除和修改

Java 比较两个集合以比较两个文本文件的添加、删除和修改,java,file,collections,io,Java,File,Collections,Io,我有两个收藏,如下所示,为学生保存ID ID是格式为111-1111的字符串。e、 g.标识221-2534、215-6365等的编号 Collection<String> newKeys = new ArrayList<String>(); Collection<String> oldKeys = new ArrayList<String>(); 在这里,文件中的条目按SSN排序。所以我相信形成的收藏也会被分类 现在: 案例:通过比较两个集

我有两个收藏,如下所示,为学生保存ID

ID是格式为111-1111的字符串。e、 g.标识221-2534、215-6365等的编号

 Collection<String> newKeys = new ArrayList<String>();
 Collection<String> oldKeys = new ArrayList<String>();
在这里,文件中的条目按SSN排序。所以我相信形成的收藏也会被分类

现在:

案例:通过比较两个集合,我想知道作为结果列表的差异。也就是说,我需要列表,其中包含的项目,得到了添加,删除和条目相同

然后,我将使用具有公共条目的列表从这两个文件中读取相应的数据,并将其与任何修改进行比较

那是在我有了共同清单之后--

a)从列表中获取id。将两个文件中此id的相应数据读入字符串。比较字符串是否存在任何差异。如果存在差异,请将新文件字符串移动到带有更新的文件中

b)在没有差异的情况下什么也不做

问题:

1)这是正确的方法吗

2)还有如何比较两个集合以获得结果列表,即。被删除,被删除和被删除

3)如何从密钥(本例中为学生id)上的文件中读取特定行

更新:

根据以下答案,添加以下代码:

Iterator<String> iOld = oldKeys.iterator();
    Iterator<String> iNew = newKeys.iterator();
    Map<String, String> tempMap = new HashMap<String, String>();

    while (iOld.hasNext()) {
        tempMap.put(iOld.next(), "old");
    }

    while (iNew.hasNext()) {
        String temp = iNew.next();
        if (tempMap.containsKey(temp)) {
            tempMap.put(temp, "both");
        }

        else {
            System.out.println("here");
            tempMap.put(temp, "new");
        }
    }
Iterator iOld=oldKeys.Iterator();
迭代器iNew=newKeys.Iterator();
Map tempMap=newhashmap();
while(iOld.hasNext()){
tempMap.put(iOld.next(),“old”);
}
while(iNew.hasNext()){
字符串temp=iNew.next();
if(临时地图容器(临时)){
tempMap.put(临时,“两者”);
}
否则{
System.out.println(“此处”);
tempMap.put(临时,“新”);
}
}
现在我有一张地图,上面有:

要比较的条目:上面地图中的条目值为“两者”

要添加的条目:上述地图中值为“新建”的条目

要删除的条目:上述地图中值为“old”的条目

所以我的问题归结为:

如何从密钥上的文件中读取特定行,以便比较它们以进行数据修改


谢谢你的阅读

如果您的文件不是太大,也许您可以执行以下步骤

  • 创建哈希映射
  • 对于旧文件中的每个条目,添加值“old”
  • 对于新文件中的每个条目,
    • 检查它是否在HashMap中
      • 如果是这样,那么设置值'Both'(另外,您可以将其添加到公共元素的HashMap中)
      • 如果不是,请将其添加为值“New”

希望这能解决问题2。请务必让我知道它是否有效。谢谢

如果您的文件不是太大,也许您可以执行以下步骤

  • 创建哈希映射
  • 对于旧文件中的每个条目,添加值“old”
  • 对于新文件中的每个条目,
    • 检查它是否在HashMap中
      • 如果是这样,那么设置值'Both'(另外,您可以将其添加到公共元素的HashMap中)
      • 如果不是,请将其添加为值“New”

希望这能解决问题2。请务必让我知道它是否有效。谢谢

你可以这样做

Collection<String> newKeys = new ArrayList<String>();  
Collection<String> oldKeys = new ArrayList<String>(); 

Collection<String> toBeDeleted = new ArrayList(oldKeys).removeAll(newKeys);
Collection<String> toBeAdded = new ArrayList(newKeys).removeAll(oldKeys);

Collection<String> sameEntries = new ArrayList(newKeys).removeAll(toBeAdded);
Collection newKeys=newarraylist();
Collection oldKeys=new ArrayList();
Collection toBeDeleted=newarraylist(oldKeys)。removeAll(newKeys);
Collection tobeaded=newarraylist(newKeys)。removeAll(oldKeys);
集合sameEntries=newarraylist(newkey).removeAll(tobeaded);
但是对于第三个问题,最好使用HashMap(如果希望自动排序键,则使用TreeMap)

***更新

在原始文件读取代码中,可以进行以下更改:

Map<String, String> oldContentMap = new HashMap<String, String>();  
while ((str = in.readLine()) != null) {       
    oldKeys.add(str.substring(0, 8).trim()); 
    oldContentMap.put(str.substring(0, 8).trim(),str.substring(8).trim());
} 
in.close(); 
Map oldContentMap=newhashmap();
而((str=in.readLine())!=null){
add(str.substring(0,8.trim());
oldContentMap.put(str.substring(0,8.trim(),str.substring(8.trim());
} 
in.close();
同样,对于新文件

  Map<String, String> newContentMap = new HashMap<String, String>();  
    while ((str = in.readLine()) != null) {       
        newKeys.add(str.substring(0, 8).trim()); 
        newContentMap.put(str.substring(0, 8).trim(),str.substring(8).trim());
    } 
    in.close(); 
Map newContentMap=newhashmap();
而((str=in.readLine())!=null){
add(str.substring(0,8.trim());
newContentMap.put(str.substring(0,8.trim(),str.substring(8.trim());
} 
in.close();
现在你可以通过

for (Map.Entry<String, String> entry : tempMap.entrySet()) { 
    if(entry.getValue().equals("both"){ //comparing for keys in both lists
         String oldContent = oldContentMap.get(entry.getKey());
         String newContent = newContentMap.get(entry.getKey());
         if(oldContent.equals(newContent)){
            System.out.println("Different data for key:"+entry.getKey());
         }
    }
}
for(Map.Entry:tempMap.entrySet()){
if(entry.getValue().equals(“两者”){//比较两个列表中的键
字符串oldContent=oldContentMap.get(entry.getKey());
字符串newContent=newContentMap.get(entry.getKey());
if(oldContent.equals(newContent)){
System.out.println(“键的不同数据:+entry.getKey());
}
}
}

您可以使用必要的temp变量,并将声明移到循环之外。

您可以这样进行

Collection<String> newKeys = new ArrayList<String>();  
Collection<String> oldKeys = new ArrayList<String>(); 

Collection<String> toBeDeleted = new ArrayList(oldKeys).removeAll(newKeys);
Collection<String> toBeAdded = new ArrayList(newKeys).removeAll(oldKeys);

Collection<String> sameEntries = new ArrayList(newKeys).removeAll(toBeAdded);
Collection newKeys=newarraylist();
Collection oldKeys=new ArrayList();
Collection toBeDeleted=newarraylist(oldKeys)。removeAll(newKeys);
Collection tobeaded=newarraylist(newKeys)。removeAll(oldKeys);
集合sameEntries=newarraylist(newkey).removeAll(tobeaded);
但是对于第三个问题,最好使用HashMap(如果希望自动排序键,则使用TreeMap)

***更新

在原始文件读取代码中,可以进行以下更改:

Map<String, String> oldContentMap = new HashMap<String, String>();  
while ((str = in.readLine()) != null) {       
    oldKeys.add(str.substring(0, 8).trim()); 
    oldContentMap.put(str.substring(0, 8).trim(),str.substring(8).trim());
} 
in.close(); 
Map oldContentMap=newhashmap();
而((str=in.readLine())!=null){
add(str.substring(0,8.trim());
oldContentMap.put(str.substring(0,8.trim(),str.substring(8.trim());
} 
in.close();
同样,对于新文件

  Map<String, String> newContentMap = new HashMap<String, String>();  
    while ((str = in.readLine()) != null) {       
        newKeys.add(str.substring(0, 8).trim()); 
        newContentMap.put(str.substring(0, 8).trim(),str.substring(8).trim());
    } 
    in.close(); 
Map newContentMap=newhashmap();
而
for(int i=0; i<oldKeys.size(); i++) {
   String oldKey = oldKeys.get(i);
   if(newKeys.contians(oldKey);
       commonKeys.put(newKeys.indexOf(oldKey) , i);
   else
       removedKeys.add(i);

}