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.
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.
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: