Latest web development tutorials

Scala Collection

Scala provides a good collection implementations provide some type of abstract collection.

Scala collection is divided into variable and immutable collections.

Variable set may be updated or expanded where appropriate. This means that you can edit, add, remove elements of a collection.

Without variable collections, by contrast, will never change. However, you can still simulate add, remove, or update operation. However, these operations will be in each case returns a new collection, while the original set does not change.

Next, we will introduce a collection of several common types of applications:

No. Collection and description
1 Scala List (list)

List feature is its storage element in a linear fashion, the collection can be stored duplicate objects.

Reference API documentation

2 Scala Set (collection)

Set is the easiest kind of collection. Objects in the collection are not sorted in a particular way, and there is no duplicate object.

Reference API documentation

3 Scala Map (map)

Map is a way to map key objects and value objects collection, its every element including a pair of key objects and value objects.

Reference API documentation

4 Scala tuple

A tuple is a set of values ​​of different types of

5 Scala Option

Option [T] represents the value of the container may contain or may not contain a value.

6 Scala Iterator (iterators)

Iterator is not a container, more precisely, is a method of accessing each element within the container.

Examples

The following code judgment, all of the above examples demonstrate the definition of the collection type:

// 定义整型 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, "w3big")

// 定义 Option
val x:Option[Int] = Some(5)