I want to compare two lists in java and build two lists oneFound and another one NotFound.
Here's my code so far, can you please advice for the best results and efficient way to do it.
So bascially i am getting a list of emails to be added or update. Now what i am try to achieve is I want to check those emails against the ones in the DB. if they exist in DB then simply update them otherwise add them.
For that I get the list of emails from the DB and then try to compare that list with the list of emails which are to be added or updated.
Then I build 2 lists one Found and another one not found. The foundones are updated and not found ones are which will be inserted into DB.
The following code builds not found with the lists which are already in db resulting in the DB insertion (causing duplicate records).
So if I correctly build found and notfound then the update and insertion will be working correctly.
public updateData (List<String> emailToAddList, List<String> emailToDeleteList)
{
List<String> emailsFromDB = Service.getEmailsFromDB();
List<String> emailToUpdateFound = new ArrayList<String>( );
List<String> emailToUpdateNotFound = new ArrayList<String>();
/**
** compare emailToAddList with emailsFromDB, if found populate
** emailToUpdateFound for data update, otherwise populate emailToUpdateNotFound for data insetion
**/
Collections.sort(emailListToAdd);
Coll开发者_开发知识库ections.sort(emailListfromDB);
if(emailListToAdd.size() > emailListfromDB.size()
{
for(String addStr: emailListToAdd)
{
if(emailListfromDB.contains(addStr))
{
emailToUpdateFound.add(addStr);
}
else
{
emailToUpdateNotFound.add(addStr);
}
}
}
else
{
for(String str: emailListfromDB)
{
if(emailListToAdd.contains(str))
{
emailToUpdateFound.add(str);
}
else
{
emailToUpdateNotFound.add(str);
}
}
}
}
Thanks
I think you will like the org.apache.commons.collections.CollectionUtils.subtract(a,b) method. This addresses your requirement in a simple way.
Here's my sample code:
public static void main(String[] args) {
List<String> input = new ArrayList<String>();
input.add("a");
input.add("b");
input.add("c");
input.add("d");
List<String> existing = new ArrayList<String>();
existing .add("d");
existing .add("b");
existing .add("z");
Collection<String> newStuff = CollectionUtils.subtract(input, existing);
Collection<String> updateStuff = CollectionUtils.subtract(input,newStuff);
System.out.println(newStuff);
System.out.println(updateStuff);
}
Judicious use of removeAll() and retainAll() is way simpler than what you're trying.
Collection<String> c1;
Collection<String> c2;
c1.removeAll(c2); // set of elements in c1 not in c2
c1.retainAll(c2); // set of elements in c1 also in c2
Treat each list as a queue. Load the "head" of each queue in a variable. Loop, examining the two head values. If left > right, left is unique (assuming you started with the lowest), so put it in the list of unique left entries, then reload left with a new value from the left list. Likewise if right > left only the other way around. If left == right then you have a match -- put them in the list of matches, then refresh both variables.
Keep this up until one list is empty.
How about adding a unique key to the email column, and then just insert anything. If the email already exist then the DB will ignore it. Then there is no need to compare 2 lists.
To approach the problem in another way...
As more an more emails are put into the database, it may become unwieldy to get the entire list from the database and then run the comparison.
If you don't need to know what emails were actually added, you can use an INSERT IGNORE
or INSERT ON DUPLICATE KEY UPDATE
(or the equivalent depending on your database) to add the entire list to the database.
If you do need to know which emails you're adding, you can do a SELECT WHERE email IN (list of new emails)
to retrieve the ones that already exist, and then subtract that list from the ones your have to get the ones that don't exist. Extending it even further, you can create a temp table, put all the emails in it, and then remove the ones that do exist via a similar query.
The above two solutions assume that the database list grows to a large number, whereas the new emails list remains at a smaller size.
精彩评论