Scala常规操作之数组、List、Tuple、Set、Map

本文会进行数组、List、元组、Set\Map的实操,有时间再回来整理格式。

数组可以是val类型,但是数据里面的具体值也是可以变的,但其实数组本身是没有变的。

一、数组

1、创建数组有两种方式:
第一种,new出来

val arr = new Array[String](3)
arr(0) = "hello"

第二种,直接赋值

val a = Array("nihao", "hello", "hi")

2、定义可变长数组(需要先引入ArrayBuffer):
import scala.collection.mutable.ArrayBuffer

val b = new ArrayBuffer[Int]
val b = new ArrayBuffer[Int]()

二、List
函数式的编程,追求状态是不可变的,也就是说创建了之后,不再更改值,上面的Array其实是可以变的,即数组即使是val类型,数据里面的具体值也是可以变的,就像一个鸡蛋,虽然里面鸡蛋里面的内容变了,但是这个鸡蛋还是这个鸡蛋。而Lists是不变的,初始化后就无法改变,修改其值会报错。

1、创建List

scala> val list = List(1, 2, 3)
list: List[Int] = List(1, 2, 3)

scala> list(1) = 4
<console>:14: error: value update is not a member of List[Int]
       list(1) = 4
       ^

scala> val otherList = List(5, 6, 7)
otherList: List[Int] = List(5, 6, 7)

拼接List可以用三个冒号:

scala> val newList = list ::: otherList
newList: List[Int] = List(1, 2, 3, 5, 6, 7)

创建List的另外一种方式,Nil表示定义为一个空List,再追加内容:

scala> val oneTwoThree = 1 :: 2 :: 3 :: Nil
oneTwoThree: List[Int] = List(1, 2, 3)

2、进行相关算子操作

  • filter、map算子操作(其实是会生成一个新的List):
scala> oneTwoThree.filter(i => i>2)
res28: List[Int] = List(3)

scala> oneTwoThree
res29: List[Int] = List(1, 2, 3)

scala> res28
res30: List[Int] = List(3)

scala> oneTwoThree.map(i => i+1)
res31: List[Int] = List(2, 3, 4)

scala> oneTwoThree
res32: List[Int] = List(1, 2, 3)

scala> res31
res33: List[Int] = List(2, 3, 4)
  • flatMap操作:
scala> oneTwoThree.flatMap(i => i to 5)
res34: List[Int] = List(1, 2, 3, 4, 5, 2, 3, 4, 5, 3, 4, 5)

1 to 5,就是1,2,3,4,5,
2 to 5,就是2,3,4,5,
3 to 5,就是3,4,5,
flatMap会将List里面的元素产生相应的元素后,会进行打平操作,所以里面的元素是同等地位的。
其实 for (i <- 0 to 2) 等价于 for (i <- 0.to(2))

  • reduceLeft算子操作:
scala> oneTwoThree.reduceLeft((a,b) => a + b)
res35: Int = 6

从左到右,依次累加,第一次循环,1+2=3,第二次继续循环,3+3=6

三、Tuple

List只能装相同类型的内容,此时可以用Tuple装不同类型的类型。

scala> val tuple = (2, "hello")
tuple: (Int, String) = (2,hello)

scala> tuple._1
res36: Int = 2

scala> tuple._2
res37: String = hello

scala> val tuple = (2, 4.44, "shaonaiyi")
tuple: (Int, Double, String) = (2,4.44,shaonaiyi)

scala> tuple._1
res38: Int = 2

scala> tuple._2
res39: Double = 4.44

scala> tuple._3
res40: String = shaonaiyi

还可以赋值,其实也是一种取值的方式:

scala> val (first, second, third) = tuple
first: Int = 2
second: Double = 4.44
third: String = shaonaiyi

此外,还可以按需要取值,如只需要去第一个:

scala> val (first, _, _) = tuple
first: Int = 2

元组tuple里面可以放22个数据。

四、Sets与Maps

Sets与Maps均含有可变与不可变两种

1、Sets操作

scala> val set = Set(1, 1, 2, 4, 5)
set: scala.collection.immutable.Set[Int] = Set(1, 2, 4, 5)

scala> set + 7
res45: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 7, 4)

scala> set
res46: scala.collection.immutable.Set[Int] = Set(1, 2, 4, 5)

scala> val newSet = set + 7
newSet: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 7, 4)

scala> set.contains(7)
res47: Boolean = false

scala> import scala.collection.mutable
import scala.collection.mutable

scala> val mutableSet = mutable.Set(1, 2, 3, 4, 5)
mutableSet: scala.collection.mutable.Set[Int] = Set(1, 5, 2, 3, 4)

scala> mutableSet += 8
res48: mutableSet.type = Set(1, 5, 2, 3, 4, 8)

scala> mutableSet
res49: scala.collection.mutable.Set[Int] = Set(1, 5, 2, 3, 4, 8)

scala> mutableSet + 7
res50: scala.collection.mutable.Set[Int] = Set(1, 5, 2, 3, 7, 4, 8)

scala> mutableSet
res51: scala.collection.mutable.Set[Int] = Set(1, 5, 2, 3, 4, 8)

而不可变的Set执行此操作,会报错的

scala> set
res52: scala.collection.immutable.Set[Int] = Set(1, 2, 4, 5)

scala> set += 7
<console>:15: error: value += is not a member of scala.collection.immutable.Set[Int]
       set += 7
           ^

2、Maps操作:

scala> val map = Map(1 -> "value1", 2 -> "value2")
map: scala.collection.immutable.Map[Int,String] = Map(1 -> value1, 2 -> value2)

scala> map + (3 -> "value3")
res54: scala.collection.immutable.Map[Int,String] = Map(1 -> value1, 2 -> value2, 3 -> value3)

scala> map
res55: scala.collection.immutable.Map[Int,String] = Map(1 -> value1, 2 -> value2)

scala> map(1)
res56: String = value1

scala> val mutableMap = mutable.Map(5 -> "value5", 6 -> "value6")
mutableMap: scala.collection.mutable.Map[Int,String] = Map(5 -> value5, 6 -> value6)

scala> mutableMap += (7 -> "value7")
res57: mutableMap.type = Map(5 -> value5, 7 -> value7, 6 -> value6)

scala> mutableMap
res58: scala.collection.mutable.Map[Int,String] = Map(5 -> value5, 7 -> value7, 6 -> value6)

遍历Maps:

scala> map.foreach{case (key, value) => println(key + " " + value)}
1 value1
2 value2

scala> mutableMap.foreach{case (key, value) => println(key + " " + value)}
5 value5
7 value7
6 value6

scala> for((key, value) <- mutableMap) {
     |   println(key + " " + value)
     | }
5 value5
7 value7
6 value6
上一篇:C++11用例


下一篇:【Python入门学习笔记】一、Python基础