Scala Collection
Scala provides a good set of collection implementations, providing some abstractions of collection types.
Scala collections are divided into mutable and immutable collections.
Mutable sets can be updated or extended where appropriate. This means that you can modify, add, and remove elements of a collection.
The immutable set class, by contrast, will 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 |
Set and description |
---|---|
1 |
The characteristic of Scala List (list) List is that its elements are storedin a linear manner, and duplicate objects can be stored in the collection. Reference API document |
2 |
Scala Set Set is the simplest kind of collection. The objects in the collection are not sorted in a specific way, and there are no duplicate objects. Reference API document |
3 |
Scala Map (mapping) Map is a set of mapping key objects and value objects, each element of which contains a pair of key objects and value objects. Reference API document |
4 |
Scala tuples are collections of different types of values |
5 |
Scala Option Option [T] Represents a container that may or may not contain avalue. |
6 |
An Scala Iterator is not a container, but rather a method of accessing the elements in the container one by one. |
Example
The following code determines that the definition examples of all the above collection types are demonstrated:
//Define an 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 tuples of two different types of elements
Val x=(10, "Runoob")
//Define Option
Val x: Option [Int]=Some (5)