Choosing the right Scala collection (Scala 2.13)

This page presents a flow-chart that will guide you in choosing your Scala collections. Feel free to contact me to correct or improve this chart.

Legend: Immutable Mutable Both

The flow chart leaves are clickable and redirect you to the official Scala documentation, in order to have more information about the class.

graph TB classDef immutable fill:#9cff99,stroke:#333,stroke-width:4px; classDef mutable fill:#f9f,stroke:#333,stroke-width:4px; classDef both fill:#99deff,stroke:#333,stroke-width:4px; A[[How to choose the right Scala Collection?]]:::title A --> NEED{You need...} NEED --> |an associative array| MAP{Keep order of
inserted elements} subgraph MAP_[scala.collection.Map] MAP --> |yes| MOO{Immutable?} MOO --> |yes| TreeSeqMap:::immutable click TreeSeqMap "" _blank MOO --> |no| LinkedHashMap:::mutable click LinkedHashMap "" _blank MAP --> |no| MSK{Sort the keys?} MSK --> |yes| SortedMap:::both click SortedMap "" _blank MSK --> |no| HashMap:::both click HashMap "" _blank end NEED --> |a collection that contain
no duplicate elements| SET{Keep order of
inserted elements} subgraph SET_[scala.collection.Set] SET --> |yes| SOO{Opposite order?} SOO --> |yes| ListSet:::immutable click ListSet "" _blank SOO --> |no| LinkedHashSet:::mutable click LinkedHashSet "" _blank SET --> |no| SSE{Sort the elements?} SSE --> |yes| SortedSet:::both click SortedSet "" _blank SSE --> |no| SNSE{Store integers?} SNSE --> |yes| BitSet:::both click BitSet "" _blank SNSE --> |no| HashSet:::both click HashSet "" _blank end NEED --> |a sequential collection of elements| SEQ subgraph SEQ_[scala.collection.Seq] SEQ{Indexed ?} SEQ --> |"yes (scala.collection.IndexedSeq)"| SII{Immutable ?} SII --> |yes| Vector:::immutable click Vector "" _blank SII --> |no| FS{Fixed size?} FS --> |yes| Array:::mutable click Array "" _blank FS --> |no| PFS{Prepend elements
and remove first?} PFS --> |yes| ArrayDeque:::mutable click ArrayDeque "" _blank PFS --> |no| ArrayBuffer:::mutable click ArrayBuffer "" _blank SEQ --> |"no (scala.collection.LinearSeq)"| NIS{Implements a...} NIS --> |Linked list ?|LLII{Immutable ?} LLII --> |yes| LLL{Lazy?} LLL --> |yes| LazyList:::immutable click LazyList "" _blank LLL --> |no| List:::immutable click List "" _blank LLII --> |no| ListBuffer:::mutable click ListBuffer "" _blank NIS --> |First-in, first-out?| Queue:::both click Queue "" _blank NIS -->|Last-in, first-out?| Stack:::mutable click Stack "" _blank NIS -->|Heap?| PriorityQueue:::mutable click PriorityQueue "" _blank end
Made with mermaid.js by Waris Radji.