1 Set collection
- Set is not representative of repeating elements of the collection.
- Set has the following properties:
- 1, the elements will not be repeated
- 2, does not guarantee the order of insertion
- The scala set is also divided into two, one is a set of immutable, the other variable is set.
1.1 immutable Set collection
- grammar
//创建一个空的不可变集
val/var 变量名 = Set[类型]()
//给定元素来创建一个不可变集
val/var 变量名 = Set[类型](元素1, 元素2, 元素3...)
- Examples
// 创建set集合
scala> val a = Set(1,1,2,3,4,5)
a: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4)
// 获取集合的大小
scala> a.size
res0: Int = 5
// 遍历集合
scala> for(i <- a) println(i)
//添加元素生成新的集合
scala> a + 6
res1: scala.collection.immutable.Set[Int] = Set(5, 1, 6, 2, 3, 4)
// 删除一个元素
scala> a - 1
res2: scala.collection.immutable.Set[Int] = Set(5, 2, 3, 4)
// 删除set集合中存在的元素
scala> a -- Set(2,3)
res3: scala.collection.immutable.Set[Int] = Set(5, 1, 4)
// 拼接两个集合
scala> a ++ Set(6,7,8)
res4: scala.collection.immutable.Set[Int] = Set(5, 1, 6, 2, 7, 3, 8, 4)
//求2个Set集合的交集
scala> a & Set(3,4,5,6)
res5: scala.collection.immutable.Set[Int] = Set(5, 3, 4)
//注意:这里对不可变的set集合进行添加删除等操作,对于该集合来说是没有发生任何变化,这里是生成了新的集合,新的集合相比于原来的集合来说发生了变化。
1.2 Variable Set collection
- To set a variable must be manually introduced: import scala.collection.mutable.Set
- Show
//导包
scala> import scala.collection.mutable.Set
import scala.collection.mutable.Set
//定义可变的set集合
scala> val set=Set(1,2,3,4,5)
set: scala.collection.mutable.Set[Int] = Set(1, 5, 2, 3, 4)
//添加单个元素
scala> set +=6
res10: set.type = Set(1, 5, 2, 6, 3, 4)
//添加多个元素
scala> set +=(6,7,8,9)
res11: set.type = Set(9, 1, 5, 2, 6, 3, 7, 4, 8)
//添加一个set集合中的元素
scala> set ++=Set(10,11)
res12: set.type = Set(9, 1, 5, 2, 6, 3, 10, 7, 4, 11, 8)
//删除一个元素
scala> set -=11
res13: set.type = Set(9, 1, 5, 2, 6, 3, 10, 7, 4, 8)
//删除多个元素
scala> set -=(9,10)
res15: set.type = Set(1, 5, 2, 6, 3, 7, 4, 8)
//删除一个set子集
scala> set --=Set(7,8)
res19: set.type = Set(1,5, 2, 6, 3, 4)
scala> set.remove(1)
res17: Boolean = true
scala> set
res18: scala.collection.mutable.Set[Int] = Set(5, 2, 6, 3, 4)
2 List List
-
List scala is the most important, is the most commonly used data structures.
-
List has the following properties:
- 1, can be saved duplicate values
- 2, the order
-
In the scala, there are two lists, one is immutable list, another list of variable
2.1 immutable list
- Immutable elements of the list is the list length are immutable
- grammar
- Use List (element 1, element 2, element 3, ...) to create an immutable list, syntax
val/var 变量名 = List(元素1, 元素2, 元素3...)
//使用 Nil 创建一个不可变的空列表
val/var 变量名 = Nil
//使用 :: 方法创建一个不可变列表
val/var 变量名 = 元素1 :: 元素2 :: Nil
- Examples
//创建一个不可变列表,存放以下几个元素(1,2,3,4)
scala> val list1=List(1,2,3,4)
list1: List[Int] = List(1, 2, 3, 4)
//使用Nil创建一个不可变的空列表
scala> val list2=Nil
list2: scala.collection.immutable.Nil.type = List()
//使用 :: 方法创建列表,包含1、2、3三个元素
scala> val list3=1::2::3::Nil
list3: List[Int] = List(1, 2, 3)
2.2 Variable list
-
The variable list is a list of elements, the length is variable.
-
To use the variable list, first import import scala.collection.mutable.ListBuffer
-
grammar
- Use ListBuffer element types create an empty variable list, grammatical structures
val/var 变量名 = ListBuffer[Int]()
- Use ListBuffer (element 1, element 2, element 3 ...) to create a variable list, grammatical structures
val/var 变量名 = ListBuffer(元素1,元素2,元素3...)
-
Examples
//导包 scala> import scala.collection.mutable.ListBuffer import scala.collection.mutable.ListBuffer //定义一个空的可变列表 scala> val a=ListBuffer[Int]() a: scala.collection.mutable.ListBuffer[Int] = ListBuffer() //定义一个有初始元素的可变列表 scala> val b=ListBuffer(1,2,3,4) b: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4)
2.3 List operations
//导包
scala> import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ListBuffer
//定义一个可变的列表
scala> val list=ListBuffer(1,2,3,4)
list: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4)
//获取第一个元素
scala> list(0)
res4: Int = 1
//获取第一个元素
scala> list.head
res5: Int = 1
//获取除了第一个元素外其他元素组成的列表
scala> list.tail
res6: scala.collection.mutable.ListBuffer[Int] = ListBuffer(2, 3, 4)
//添加单个元素
scala> list +=5
res7: list.type = ListBuffer(1, 2, 3, 4, 5)
//添加一个不可变的列表
scala> list ++=List(6,7)
res8: list.type = ListBuffer(1, 2, 3, 4, 5, 6, 7)
//添加一个可变的列表
scala> list ++=ListBuffer(8,9)
res9: list.type = ListBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9)
//删除单个元素
scala> list -=9
res10: list.type = ListBuffer(1, 2, 3, 4, 5, 6, 7, 8)
//删除一个不可变的列表存在的元素
scala> list --=List(7,8)
res11: list.type = ListBuffer(1, 2, 3, 4, 5, 6)
//删除一个可变的列表存在的元素
scala> list --=ListBuffer(5,6)
res12: list.type = ListBuffer(1, 2, 3, 4)
//可变的列表转为不可变列表
scala> list.toList
res13: List[Int] = List(1, 2, 3, 4)
//可变的列表转为不可变数组
scala> list.toArray
res14: Array[Int] = Array(1, 2, 3, 4)