Skip to main content

Deal with Java Collection Framework



Deal with Java Collection Framework

We are sure every java developer is aware of Collection framework and we have seen some time they get confused when they are about to use the JAVA collection API. In this post, we will help you to sort your confusion regarding the usage of Java collection API. 

Developer Problem 

  • We are sure every java developer is aware of Collection framework and we have seen some time they get confused when they are about to use the JAVA collection API.
  • The main concern is, what exactly they need to use either map, set, tree or list...

Solution:


Before selecting the collection API, the developer needs to ask a few self questions

  • Is it OK if my data set is duplicate(eg  Ram, Rahim, Ram, Raj)?
  • Do I need to maintain the order(eg list of railway stations from source to destination, please note here the order of station need to maintain)?
  • DO I need my data in a sorted way(eg the data set of the top scorer in a cricket match?

Based on the above answer you can select the below collection.
If you don't want any duplicate data, you can always opt 
  • If you don't bother about an order, duplication of data, then you can opt LIST(ArrayList)
  • If you don't want duplication of data then you must use Set(HashSet)
  • If your data is in key-value pair, please go on with Map(HashMap)
  • if you want to maintain the order then preferable is Linked***..(i.e LinkedList, LinkedHashMap, LinkedHashSet)


*I hope you like this content, if you need any info on other topics, please let me know.

Comments

Popular posts from this blog