This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi Cyril, Welcome to Javaranch. (FYI: Code tags have square brackets and not angle brackets. More information here)
Yeah, your analysis is correct. The time taken by removeAll() is directly dependent on the size of the list to be removed. This is the code that it uses:
The code in bold is what removeAll() does in addition to what your simple removal does. Since, the size of the passed collection is huge, the contains method call will take a long time and this time will be multiplied by the number of elements in the list that is being modified.
I suggest a few solutions (may not be the best possible):
If you are removing all the elements from the collection then clear() will be the best thing to do.
If you do not need the return value of the removeAll() operation, you can use the code that you have written i.e. do a simple iterate and call remove for all the items to be removed.
Actually, I'm not really a newcomer of Java Ranch but I post rarely, and each time I forget my login and the mail account I used to register...
Your analysis is correct, and c.contains is probably the culprit. I can't use clean(), since there may be 2 or 3 items different in the real scenario of my point, but I'll go with the solution with the loop and the simple "remove".
Originally posted by Stephane Clinckart: I could not remove the smiley... also by using the code tags :-/
[ March 11, 2008: Message edited by: Stephane Clinckart ]
Check the "Disable smilies in this post." checkbox under "options" in the edit screen...
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus