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

前端 未结 4 1015
鱼传尺愫
鱼传尺愫 2020-12-21 21:36

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.

 Collec         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-21 21:56

    I would do your task in this way

    • Create two HashMap one for each file(oldFile, newFile), your ids will be the keys of the map
    • Build new arraylists: common, toBeAdded, toBeDeleted
    • loop on oldKeysHashMap keys: for each key check whether the key exists in the newHasMap. If yes check if the two keys contain the same value (this is easy with Maps) -> put the entry in common arraylist. If no put the entry into toBeDeleted.
    • loop on newKeysHashMap and fill in the toBeAdded arrayList
    • Mix the toBeAdded and Common arraysList in a new one. Delete the two original files. Write a new file and fill in the file with the entries of the new mixed arrayList. (deleting and creating a new file should be more qick than searching the ids in the file and removing the line)

    I can provide also some code snippet. If you need use an implementation of Map interface that keeps entry sorted. This is not that case of HashMap, SortedHashMap could be the right one.

提交回复
热议问题