Answer to Question #167205 in Java | JSP | JSF for Baudouin Eric

Question #167205

When writing a data structure, what should be our guidelines for choosing the right Java Collection?


1
Expert's answer
2021-02-27T11:55:00-0500

The guidelines that should be followed while choosing a Java collection is given as follows :

  1. Select the most general type of data organisation according to the type of data that is needed. Example : list or map.
  2. Choose the type of implementation that simply just satisfies your requirements. For example : If there is no need of sorted data, then don’t include sorting process.

These Use Cases should also be kept in mind :

List: When we need to store or iterate through a number of things, then List is used.

Set: When the items that have already been processed need to be remembered in order to take yes or no decision, then Set can be used.

Map: When the case need to be stored as key-value pairs or association.

Queue: When there is need to have different queues in order to perform different functions like sequential data processing and different compression techniques etc., Queue can be used.

These were the basic criteria or guidelines. Besides these, some other basic guidelines or pointers are as follows :

  1. If there are only values with other conditions in the case -
  • ArrayList can be used if case contains only values
  • Linked HashSet can be used if there is no need of duplicacy of values.
  • Linked HashSet can be used if there is need to maintain an insertion order.
  • Tree Set can be used if sorted pair are needed.
  1. If there are key value pair with other conditions in your case -
  • HashMap can be used if your case contains only key-value pair.
  • Linked HashMap can be used if there is need to maintain insertion sort as well.
  • Use Tree Map if there is need of sorted pairs.

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Alie
19.12.23, 03:24

this is amazing touch on the topic. I really enjoyed the material. Thank you for this great research paper.

Leave a comment

LATEST TUTORIALS
New on Blog