Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala collections flowchart

There is a nice flowchart (taken from here) for choosing a particular container in C++:

container-flowchart

Is there something similar for the Scala collections? I'm still somewhat overwhelmed with the options.

like image 995
fredoverflow Avatar asked Jun 28 '14 07:06

fredoverflow


1 Answers

I am not aware of such flowcharts for Scala, but I guess one would be useful. I made one for you -- larger picture here.

Note that there is some added complexity, since Scala has more collections and there is both the mutable and the immutable package. Where possible, I added both alternatives to the rectangle.

I tried to follow the C++ STL flow diagram as much as possible, but I thought that the lower left part was complicating things a bit too much, so I changed the flow there slightly.

EDIT: fixed some typos.

EDIT: As Travis, suggested, note that in a majority of situations, you only need to pick between a Map, Set, List, ArrayBuffer or a Vector.

  • if you need key-value lookup, use a Map
  • if you need to check for presence of elements, use a Set
  • if you need to store elements and traverse them, use a List or an ArrayBuffer
  • if you don't need a persistent collection, but random access is really important, use ArrayBuffer
  • if you need relatively fast random access and persistent sequences, use Vector

If that does not help and you have a more exotic use-case, use this chart.

enter image description here

like image 161
axel22 Avatar answered Sep 25 '22 02:09

axel22