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.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade