Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7674157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:40:35+00:00 2026-05-31T16:40:35+00:00

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

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:40:36+00:00Added an answer on May 31, 2026 at 4:40 pm

    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);
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two collections which have property Email in both collections. I need to
I have a class which has two HashSet<String> collections as private members. Other classes
I have implemented the two classes shown at http://tomcat.apache.org/tomcat-6.0-doc/aio.html which gives a messenger application
I have a collection of records. Which have two boxers, match date, location etc...
I have two Collection objects, I want to associate each object of these two
I have a Project Collection setup in my TFS2010RC deployment. I have two Projects
Can I contain two different types in a collection? For example, can I have
I have two tables, and they are using different collations. It is not allowed
Say, I have two computers behind firewalls, routers, etc (ie. no incoming connections). Is
Have two folders with approx. 150 java property files. In a shell script, how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.