Friday, May 27, 2011

Significance of selecting a proper collection class OR danger of using collections with hashing

In java, lot of people are fond of the ‘Set’ collection class. The most commonly used implementation class of Set is HashSet. It is easy to use and is believed to have good performance. It is good for returning data from the data access layer.(though it may not be of use with ORDERBY clauses).

But one thing people might forget is that it uses hashing algorithm. So the order in which you store the data is not maintained. There are instances where we get different results when executed at different times. This could be because of iterating through a collection such as HashSet. There are chances of its usage going unnoticed and at times it becomes difficult to debug it out. But as a good practice, it is better to use some predictable collections like ArrayList unless absolutely necessary.

Performance may not be a big issue with the blinding fast advancements in the processing and storage technologies. But making the application work consistently and predictably is the most important responsibility of a developer. Definitely there is no use of a software which works very fast and consumes very less memory if it doesn't do what is expected.

0 comments: