java - 比较两个集合用于比较两个文本文件的添加、删除、修改

标签 java file collections io

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

id 是格式为 111-1111 的字符串。例如ID 221-2534、215-6365 等。

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

ID 与其他数据一起采用固定格式文件。即前 8 个字符 ID、接下来的 10 个字符名称、接下来的 10 个字符地址等。

我正在将 ids 读入集合,如下所示:

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

此处文件中的条目按 SSN 排序。所以我相信形成的集合也会被排序。

现在:

案例:我想通过比较两个集合来了解结果列表的差异。也就是说,我需要包含添加的条目、删除的条目和相同的条目的列表。

然后,我将使用具有公共(public)条目的列表从两个文件中读取相应的数据,并比较其是否有任何修改。

那是在我有了通用列表之后 -

a) 从列表中获取一个 ID。从两个文件中将该 id 的相应数据读取到字符串中。比较字符串是否有任何差异。如果存在差异,请将 newFile 字符串移至 fileWithUpdates 中。

b) 如果没有差异,则不执行任何操作。

问题:

1) 这是正确的方法吗?

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

3) 如何从 key (本例中为学生 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");
        }
    }

现在我有了一张 map ,其中包含:

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

要添加的条目:上面 map 中值为“new”的条目

要删除的条目:上面 map 中值为“old”的条目

所以我的问题归结为:

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

感谢您的阅读!

最佳答案

总的来说,我认为这不是正确的方法。我不会将所有信息存储在单个字符串中,而是创建一个对象,其中包含您需要存储的各种内容的字段。

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

  }

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

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++;
   }
}

您需要稍微调整上面的代码以使其安全。另一种方法是使用 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);

}

关于java - 比较两个集合用于比较两个文本文件的添加、删除、修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9766720/

相关文章:

java - view.setText 发生在循环之后?

java - 来自配置文件或 .java 的巨大数组

java - 在尚未完成复制/上传时读取文件内容

java - Guava:copyOf() 方法的 ImmutableList 魔法

java - 字符串输入的背包解决方案

java - testcontainers - 由于测试失败,maven 构建失败

java - 在 Java 中有效地将文件从 URL 读取到 byte[]

c - 数组类型的元素类型不完整,无法将结构数组传递给函数

java - 有没有比保留相同功能的三个嵌套 map 更好的解决方案?

java - 链接mysql和netbeans数据时出错