Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

java - comparing two collections for comparing two text files for additions, deletions, modifications

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...