Skip to main content

Collections

Deep dive on the different collections available in Scala and Java

Scala Collections

CollectionDescriptionSimilar
ArrayBufferMutable collectionsStringBuilder
SeqOrdered collection of elements
ListLinked list, faster inserts, slower random accessNonEmptyList, LazyList
VectorIndexed sequence, faster random access
MapKey value pairs, for fast lookupsHashMap, TreeMap
ListMapSorted map
QueueUsed when FIFOArrayDeque, PriorityQueue
SetUnordered collection of unique elementsHashSet, TreeSet
ChainOptimized for adding elements at both the head and tailNonEmptyChain

From Scala docs

Collections

Immutable collections:

Immutable Collections diagram

Mutable collections:

Mutable Collections diagram

Java Collections

CollectionDescriptionSimilar
ArrayListMutable - Best for random access and read operationsScala Vector
LinkedListBest for frequent insertions and deletionsScala List
HashSetNo duplicates and no order of elementsScala Set
TreeSetSorted - Best for sorted unique elements and range-based queriesScala SortedSet
HashMapstores key-value pairs and does not guarantee any specific orderScala Map
TreeMapStores keys in sorted orderScala SortedMap