Question

I have gone through Java Collections elements- List, Set and Maps. According to my research, I found:

enter image description here

Are my findings correct or not? Actually I wanted to know about the performance issues of these collections. What would be the performance if I have to add, retrieve and search data in each collections?

Was it helpful?

Solution

There is already a hint in your question.

  • You mention "Depends on implementation". All you mentioned are the interfaces and the question is very abstract. Which one to choose over the rest depends on your particular use case.
  • For example, the List implementations LinkedList and ArrayList differ in their behaviour when considered their usage for frequent modification, random access etc.
  • Similarly, for Map there are differences between their behaviour. Here is link for their performance comparison.
  • Searching in TreeMap/Set will always be faster as the entities are already sorted. So it all depends on what you need. Based on that select the appropriate implementation.

OTHER TIPS

Performance depends on use case scenario. For example if you have records which will be update(deleted, inserted etc.) many times you should use linked list. If you will be iterate over and over large list you should try ArrayList. If you need to get specify(by id for example) record then the best option is Map. So the performance of collection depends on real scenario. The best way to learn about performance is implements couple use case and check which collection have better times depends on scenario. This is interesting site: http://www.programcreek.com/2013/03/arraylist-vs-linkedlist-vs-vector/

I think you are quite rigth on your findings. In the web http://bigocheatsheet.com/ you can see the difference between them.

As examples of use of each datastructures, you used Maps when you have a clear relationship between key and value, for instance when you want to keep record of users and each user have a unique id, Sets are usefull for keeping record of things that are not going to repeat apearances, for instance when you have some kind of enumerate but you need to keep more information than the name of the enumerate and finnally the list is the default datastructure to keep record of lists without much restrictions or limitations, but also is one of the worst in efficiency matter.

I strongly suggest you to keep that page in your bookmarks and take a look every time you want to know what should you use until you are familiar with them.

Cheers.

PD: Maps are often disassemble into Lists or Sets with objects for the matter of compatiblity, for example with JPA (Maps are often much harder to use than a list in that enviroments).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top