Java collections : Are you making the right choice?

A number of times in java programming a point arises when you think, am I using the right implementation for this data ? And a state of uncertainty sets in. Well, here is a basic set of questions you need to ask yourself and then decide the right collection for your data.

  • Does it allow duplicate elements?
  • Does it accept null?
  • Does it allow accessing elements by index?
  • Does it offer fast adding and fast removing elements?
  • Does it support concurrency?

The difference between Set, List and Map can be clearly understood by keeping these 6 points in mind.

1. SET :
 a : Set does not store duplicate objects.
 b : It prints the stored objects without any order.
2. LIST :
 a : List can store duplicate objects.
 b : It prints stored objects in ordered format.
3. MAP :
 a : Stores data in key-value pair, hence every inserted key had to be unique.
 b : It prints stored objects in ordered format.

Hope, this will help you make the right choice.