Let's say I have a list A that needs to look exactly like list B. All the objects that B has but A does not have need to be added to A. And all the objects that A has but not B, need to be removed from A.
The reason why I need this is because I have an ArrayList of Players that I'm saving to a file. Every time I update an attribute of a Player, I save the changes to the file by calling a method that looks to the ArrayList of Players and saves it. This works because the ArrayList has the references to the Players.
However, whenever I search for a Player in the list, I first update the list by reading the file where its stored. This replaces all the references with whole new objects. After I do this, if I make a change to a previously fetched user and try to save it. The new instance of the Player gets saved instead of the one I made the changes in.
Would coming up with a good algorithm to make a list equal to another the solution? Or is there a better way to update an entire list while keeping references in use there?
UPDATE: Updated solution, runs in O(nlogm) time. Loops through each element in destination, searches for it in source. If it's found, removed from source. If not, removed from destination. Then add remaining elements in source to destination. List needs to be sorted of course, but the list that I get from the file will already be sorted because I sort as I add.
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CopyList {
public static void copyList(List dest, List src) {
copy(dest, src);
// the remaining elements in src list will be those that were originally
// in src but not in dest and so they need to be added
dest.addAll(src);
}
public static void copyList(List dest, List src, Verify v) {
copy(dest, src);
// the remaining elements in src list will be those that were originally
// in src but not in dest and so they need to be added
addAll(dest, src, v);
}
public static void copyList(List dest, List src, Comparator c) {
copy(dest, src, c);
// the remaining elements in src list will be those that were originally
// in src but not in dest and so they need to be added
dest.addAll(src);
}
public static void copyList(List dest, List src, Comparator c, Verify v) {
copy(dest, src, c);
// the remaining elements in src list will be those that were originally
// in src but not in dest and so they need to be added
addAll(dest, src, v);
}
private static void copy(List dest, List src) {
// go through dest list to search if every element is in the new list
// travel backwards through dest because we will be removing elements from it
for(int i = dest.size()-1; i >= 0 ; i--) {
int src_i = Collections.binarySearch(src, dest.get(i));
if(src_i >= 0)
// if element is found in src list, remove it from src list
src.remove(src_i);
else
// if element is NOT found in src list, remove it from dest list
dest.remove(i);
}
}
private static void copy(List dest, List src, Comparator c) {
// go through dest list to search if every element is in the new list
// travel backwards through dest because elements might be removed
for(int i = dest.size()-1; i >= 0 ; i--) {
int src_i = Collections.binarySearch(src, dest.get(i), c);
if(src_i >= 0)
// if element is found in src list, remove it from src list
src.remove(src_i);
else
// if element is NOT found in src list, remove it from dest list
dest.remove(i);
}
}
private static void addAll(List dest, List src, Verify v) {
// verify each element in src list before adding it to dest list
for(Object o: src)
if(v.verify(o))
dest.add(o);
}
}