English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Scala provides a good set of collection implementations, offering some abstract types of collections.
Scala collections are divided into mutable and immutable collections.
Mutable sets can be updated or expanded at appropriate places. This means you can modify, add, or remove elements from a set.
In contrast, immutable collection classes will never change. However, you can still simulate addition, removal, or update operations. But these operations will return a new collection in each case, while the original collection does not change.
Next, we will introduce several applications of commonly used collection types:
Serial Number | Collections and Description |
---|---|
1 | Scala List (List) The characteristic of List is that its elements are stored linearly, and the collection can store duplicate objects. Reference API Documentation |
2 | Scala Set (Collection) Set is the simplest type of collection. Objects in the collection are not sorted in a specific way, and there are no duplicate objects. Reference API Documentation |
3 | Scala Map (Mapping) Map is a collection that maps key objects to value objects, with each element containing a pair of key and value objects. Reference API Documentation |
4 | Scala Tuples Tuples are collections of values of different types |
5 | Scala Option Option[T] represents a container that may or may not contain a value. |
6 | Scala Iterator (Iterators) An iterator is not a container; it is more accurate to say it is a method for sequentially accessing elements within a container. |
The following code demonstrates the definition examples of all the above collection types:
// Define Integer List val x = List(1,2,3,4) // Define Set val x = Set(1,3,5,7) // Define Map val x = Map("one" -> 1, "two" -> 2, "three" -> 3) // Create a tuple with two elements of different types val x = (10, "w3codebox) // Define Option val x: Option[Int] = Some(5)