Scala Collection
Scala provides a good set of collection implementations and provides some abstractions of collection types.
Scala collections are divided into mutable and immutable collections.
Mutable collections can be updated or extended where appropriate. This means you can modify, add, and remove elements of a collection.
Immutable collection classes, in contrast, never change. However, you can still simulate add, remove or update operations. But these operations will return a new collection in each case, leaving the original collection unchanged.
Next we will introduce the applications of several common collection types:
Serial number | Collection and description |
---|---|
1 | Scala List(List) The characteristic of List is that its elements are stored in a linear manner, and repeated objects can be stored in the collection. Reference API Document |
Scala Set (set) | Set is the simplest kind of collection. The objects in the collection are not ordered in a particular way, and there are no duplicate objects. Refer to API documentation |
Scala Map (mapping) | Map is a mapping of key objects and value objects A collection whose each element contains a pair of key objects and value objects.Reference API documentation |
Scala Tuple | A tuple is a collection of values of different types|
Scala Option | Option[T] represents a container that may or may not contain a value.|
Scala Iterator (Iterator) | An iterator is not a container, but rather a method of accessing the elements in the container one by one.
The following code judgment demonstrates the definition examples of all the above collection types:
// 定义整型 List val x = List(1,2,3,4) // 定义 Set var x = Set(1,3,5,7) // 定义 Map val x = Map("one" -> 1, "two" -> 2, "three" -> 3) // 创建两个不同类型元素的元组 val x = (10, "php") // 定义 Option val x:Option[Int] = Some(5)