0

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?

Chandra Prakash
  • 781
  • 4
  • 13
  • 23
  • Keep googling !! your question is very broad and cant be explained withsimple answers – Santhosh May 05 '14 at 06:51
  • 6
    Or rather, stop googling, and simply read the reference documentation of each collection. It makes no sense to have a column for "Performance". You choose a collection first for its functionality. And the performance then varies depending on the concrete type of the collection, and on the method invoked. The reference doc gives the BigO of each operation of each collection. – JB Nizet May 05 '14 at 06:54
  • Try all of these, Find what is good for you. – ѕтƒ May 05 '14 at 06:54
  • "Actually I wanted to know about the performance issues of these collections." You probably should have made this the main point of your question instead of saving it for the last part of your question. – awksp May 05 '14 at 07:05
  • I totally agree with @JBNizet. The data structures in Collections are chosen based on "what you want" - duplicates allowed?, dictionary?, "no duplicates"?. Once you know your requirement completely, you won't have MANY options to choose from. Its just like - am I just reading the list or am I both reading and inserting into the list ?. Depending on this, you might use ArrayList or LinkedList. – TheLostMind May 05 '14 at 07:06
  • Also, [Big O cheat sheet](http://bigocheatsheet.com/#data-structures). You have to be a little careful though because the specific implementation makes some operations more expensive than the technically should be (e.g. `LinkedList` remove()) – awksp May 05 '14 at 07:06
  • These three interfaces are all very different from each other in what they achieve. Think of them as like "cucumber", "toaster" and "spice jar". The fact that they all belong in your kitchen doesn't really mean that they have anything in common. So it's pretty meaningless to compare the performance of each. Which one is right to use depends on whether you want to add something green to a salad, heat bread or store cinnamon. – Dawood ibn Kareem May 05 '14 at 07:08
  • Yeah I agree with @JB Nizet, and http://bigocheatsheet.com/#data-structures seems helpful too. Would you refer some helpful sites for research? – Chandra Prakash May 05 '14 at 07:17

3 Answers3

0

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/

0

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.
Community
  • 1
  • 1
Santosh
  • 17,667
  • 4
  • 54
  • 79
0

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).

Koalk
  • 59
  • 1
  • 10