ArrayList Vs LinkedHashSet - Perforamce is the key!!

No I am not discussing about the differences between these two great collection classes. I am just posting an intersting experience that I had when using these two in terms of performance.

There was a requiement where we had two collections and and had to compare these two collections and retain only the elements in this collection(say A) that are contained in the specified collection(say B).

Blindly we went ahead and used ArraList.retainAll() method as we had declared the two collections as ArrayList( Why we chose Arraylist over LinkedList, Vector, Java classic Array, etc. is out of context in this scenario). The data in these collections was so huge that to do this it was taking a huge 51 minutes.. Phew..

Obviously this was a performance bug raised by our performance team. We were unable to understand why this was happenening. As we researched, we found that there is performance problem when using ArrayList( No using vector would not have worked).

We found out that the implemetation of retainAll() method was reimplemented(Overridden) in LinkedHashSet with a different algorithm. We tried using the same and voila, the response time came down drastically to 25 seconds. We were shocked and were not ready to believe the same. But after thourough testing we breathed easy and concluded that retainAll() method in LinkedHashSet is much faster than the one in ArrayList.

If you guys have any information on this do let me know @ girish.ahankari@gmail.com.

Goto HOME

Click Here to view Archives(Previous questions) of Java Interview Questions
Post your Resumes
HERE
Join javaquestions Group
Google
The Advertising Network

Disclaimers: No claims are made about the accuracy of any of the document presented in this site and no responsibility is taken for any errors. All the links provided in this site are individual compilation, however no claims are made about the accuracy and authenticity of the respective contents(including java interview questions).