比较两个集合以比较两个文本文件的添加,删除,修改 [英] comparing two collections for comparing two text files for additions, deletions, modifications

查看:412
本文介绍了比较两个集合以比较两个文本文件的添加,删除,修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个集合,其中包含学生的ID。



ids是111-1111格式的字符串。例如ids 221-2534,215-6365等。

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

ID与其他数据一起存储在固定格式的文件中。这是前8个char id,下10个字符名,下10个字符addr等。



我正在读取ids到集合如下:

  String oldFile =C:\\oldFile.dat; 
String newFile =C:\\\\
ewFile.dat;
BufferedReader in;
String str;
//从旧文件读取密钥
in = new BufferedReader(new FileReader(oldFile));
while((str = in.readLine())!= null){
oldKeys.add(str.substring(0,8).trim());
}
in.close();

//从新文件读取密钥
in = new BufferedReader(new FileReader(newFile));
while((str = in.readLine())!= null){
newKeys.add(str.substring(0,8).trim());
}
in.close();

这里文件中的条目在SSN上排序。



现在:



案例:我想通过比较两个集合来了解差异作为结果列表。这是我需要列表,其中包含已添加的条目,已删除的条目和相同的条目。



然后我将使用具有公共条目的列表来读取相应的数据



这是我有共同的列表后 -



a)从列表中获取ID。从两个文件中读取此ID的相应数据到字符串。比较任何差异的字符串。如果有区别,请将newFile字符串移动到fileWithUpdates。



b)

问题:



1)这是正确的方法吗?



2)此外,如何比较两个集合以获得结果列表。 toBeDeleted,toBeAdded和sameEntries?



3)如何从密钥(在这种情况下是学生ID)的文件中读取特定行?



更新:



根据以下答案,添加了以下代码: / p>

 迭代器< 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);
}
}



现在我有一张地图: p>

要比较的条目:上方地图中的值为both的条目



strong>要添加的条目:上面的值为new的条目



要删除的条目:地图的值为old



所以我的问题归结为:



如何读取特定行一个文件上的键,使我可以比较它们的数据修改?



感谢阅读!

解决方案

认为这是正确的做法。

  

> public Student {
String id; //或int,或char [8]
String firstName,lastName;
字符串地址;
//等等

//构造函数 - 给定一个来自数据文件的输入行,创建一个Student对象
public Student(String line){
id = line.substring(0,8);
//等等

}

这两个集合,我们将它们都声明为ArrayList,然后跟踪它们共同的索引。

  ArrayList< String> newKeys = new ArrayList<>(); // java 7 syntax 
ArrayList< String> oldKeys = new ArrayList<>();
//从文件存储键。

TreeMap< Integer,Integer> commonKeys = new TreeMap< Integer,Integer>();
//将来自newList的索引值存储为映射到旧列表索引的键。

ArrayList< Integer> removedKeys = ArrayList<>();
//存储不在newKeys中的oldKeys的索引。

int newListIndex = 0;
int oldListIndex = 0;
while(newListIndex< newKeys.size()&& oldListIndex< oldKeys.size()){
if(newKeys.get(newListIndex).equals(oldKeys.get(oldListIndex)){
commonKeys.put(newListIndex,oldListIndex);
oldListIndex ++; newListIndex ++
}
else if(newKeys.get(newListIndex).compareTo(oldKeys.get(oldListIndex)> ){
removedKeys.add(oldListIndex);
oldListIndex ++
}
else {
//也许这是一个不在旧列表中的newListIndex,
newListIndex ++;
}
}

另一种方法是使用如下的contains方法:

 需要调整上述代码以使其失效, 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);

}


I have two collections as below which hold IDs for Students.

The ids are Strings in the format 111-1111. e.g. of ids 221-2534, 215-6365, etc.

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

The ids are in a fixed format file along with other data. That is first 8 char ids, next 10 char name, next 10 char addr, etc.

I am reading ids into collection as below:

String oldFile = "C:\\oldFile.dat";
String newFile = "C:\\newFile.dat";
BufferedReader in;
String str;
// Read keys from old file
in = new BufferedReader(new FileReader(oldFile));
while ((str = in.readLine()) != null) {
      oldKeys.add(str.substring(0, 8).trim());
}
in.close();

// Read keys from new file
in = new BufferedReader(new FileReader(newFile));
while ((str = in.readLine()) != null) {
    newKeys.add(str.substring(0, 8).trim());
}
in.close();   

Here the entries in the file are sorted on SSN. So I believe the collections formed will also be sorted.

Now:

Case: I want to know the differences as resultant lists by comparing the two collections. That is I need lists which contains entries which got added, entries which got removed and entries which are same.

I will then use the list having common entries to read corresponding data from both files and compare that for any modifications.

That is after I have the common list --

a) Take a id from the list. Read the corresponding data for this id from both files into Strings. Compare the String for any differences. In case of a difference, move the newFile String into a fileWithUpdates.

b) Do nothing in case of no difference.

Questions:

1) Is this correct approach ?

2) Also how to compare the two collections to get resultant lists viz. toBeDeleted, toBeAdded and sameEntries ?

3) How to read a specific line from a file on a key (student id in this case) ?

Update:

Based on below answer, added the below code:

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");
        }
    }

So now I have a map which has:

Entries to be compared: Entries in above map with value "both"

Entries to be added: Entries in above map with value "new"

Entries to be deleted: Entries in above map with value "old"

So my problem boils down to:

How to read a specific line from a file on a key so that I can compare them for data modifications??

Thanks for reading!

解决方案

Overall, I don't think this is the correct approach. Instead of storing all the information in a single String, I would create an object with fields for the various things you need to store.

public Student {
   String id; //or int, or char[8]
   String firstName, lastName;
   String address;
  //and so on

  //constructor - Given a line of input from the data file, create a Student object
  public Student(String line) {
     id = line.substring(0,8);
     //and so on

  }

As for comparing the two collections, let's declare them both as ArrayLists and then keep track of the indices of what they have in common.

ArrayList<String> newKeys = new ArrayList<>();  //java 7 syntax
ArrayList<String> oldKeys = new ArrayList<>();
//store keys from files.

TreeMap<Integer, Integer> commonKeys = new TreeMap<Integer, Integer>();
//stores the index values from newList as keys that get mapped to the old list index.

ArrayList<Integer> removedKeys =ArrayList<>();  
// Store the indices from oldKeys that are not in newKeys.

int newListIndex = 0;
int oldListIndex = 0;
while(newListIndex < newKeys.size() && oldListIndex<oldKeys.size()) {
   if(newKeys.get(newListIndex).equals(oldKeys.get(oldListIndex) ) {
      commonKeys.put(newListIndex,oldListIndex);
      oldListIndex++; newListIndex++ 
   }
   else if(newKeys.get(newListIndex).compareTo(oldKeys.get(oldListIndex)>0 ) {
      removedKeys.add(oldListIndex);
      oldListIndex++
   }
   else {
      //maybe this is a newListIndex that is not in the old list, so it was added.
      newListIndex++;
   }
}

You will need to tweak the above code a bit to make it fail-safe. Another approach is to use the contains method like this:

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);

}

这篇关于比较两个集合以比较两个文本文件的添加,删除,修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆