scala映射和元组
This commit is contained in:
parent
6ed20dc54e
commit
27efac62b2
@ -271,4 +271,4 @@ res6: Boolean = true
|
||||
|
||||
## 参考资料
|
||||
|
||||
1. Martin Odersky(著),高宇翔(译) . Scala编程(第3版)[M] . 电子工业出版社 . 2018-1-1
|
||||
1. Martin Odersky . Scala编程(第3版)[M] . 电子工业出版社 . 2018-1-1
|
||||
|
@ -1,5 +1,6 @@
|
||||
# Scala 数组相关操作
|
||||
<nav>
|
||||
# Scala 数组相关操作
|
||||
|
||||
<nav>
|
||||
<a href="#一定长数组">一、定长数组</a><br/>
|
||||
<a href="#二变长数组">二、变长数组</a><br/>
|
||||
<a href="#三数组遍历">三、数组遍历</a><br/>
|
||||
@ -8,189 +9,189 @@
|
||||
<a href="#六与Java互操作">六、与Java互操作</a><br/>
|
||||
</nav>
|
||||
|
||||
## 一、定长数组
|
||||
|
||||
在Scala中,如果你需要一个长度不变的数组,可以用Array。
|
||||
|
||||
```scala
|
||||
// 10个整数的数组,所有元素初始化为0
|
||||
scala> val nums=new Array[Int](10)
|
||||
nums: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
|
||||
|
||||
// 10个元素的字符串数组,所有元素初始化为null
|
||||
scala> val strings=new Array[String](10)
|
||||
strings: Array[String] = Array(null, null, null, null, null, null, null, null, null, null)
|
||||
|
||||
// 使用指定值初始化,此时不需要new关键字
|
||||
scala> val a=Array("hello","scala")
|
||||
a: Array[String] = Array(hello, scala)
|
||||
|
||||
// 使用()来访问元素
|
||||
scala> a(0)
|
||||
res3: String = hello
|
||||
```
|
||||
|
||||
这里需要注意以下两点:
|
||||
|
||||
+ 在scala中使用`(index)`而不是`[index]`来访问数组中的元素,因为访问元素,对于Scala来说是方法调用,`(index)`相当于执行了`.apply(index)`方法。
|
||||
+ scala中的数组与Java中的是等价的,`Array[Int]()`在虚拟机层面就等价于Java的`int[]`。
|
||||
|
||||
|
||||
|
||||
## 二、变长数组
|
||||
|
||||
在scala中,通过ArrayBuffer实现变长数组(又称缓冲数组),相当于Java中的ArrayList。变长数组的构建方式及常用操作如下:
|
||||
|
||||
```java
|
||||
import scala.collection.mutable.ArrayBuffer
|
||||
|
||||
object ScalaApp {
|
||||
|
||||
// 相当于Java中的main方法
|
||||
def main(args: Array[String]): Unit = {
|
||||
// 1.声明变长数组(缓冲数组)
|
||||
val ab = new ArrayBuffer[Int]()
|
||||
// 2.在末端增加元素
|
||||
ab += 1
|
||||
// 3.在末端添加多个元素
|
||||
ab += (2, 3, 4)
|
||||
// 4.可以使用++=追加任何集合
|
||||
ab ++= Array(5, 6, 7)
|
||||
// 5.缓冲数组可以直接打印查看
|
||||
println(ab)
|
||||
// 6.移除最后三个元素
|
||||
ab.trimEnd(3)
|
||||
// 7.在第1个元素之后插入多个新元素
|
||||
ab.insert(1, 8, 9)
|
||||
// 8.从第2个元素开始,移除3个元素,不指定第二个参数的话,默认值为1
|
||||
ab.remove(2, 3)
|
||||
// 9.缓冲数组转定长数组
|
||||
val abToA = ab.toArray
|
||||
// 10. 定长数组打印为其hashcode值
|
||||
println(abToA)
|
||||
// 11. 定长数组转缓冲数组
|
||||
val aToAb = abToA.toBuffer
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
这里需要说明的是:由于scala的变长数组是使用java的集合来实现的,而Java集合的底层实现是链表,这意味使用`+= `在末尾插入元素是一个高效的操作,因为其时间复杂度是O(1)。而使用insert随机插入元素的时间复杂度是O(n),因为在其插入位置之后的所有元素都要进行对应地后移,所以在`ArrayBuffer`中随机插入元素是一个低效的操作。
|
||||
|
||||
## 三、数组遍历
|
||||
|
||||
```scala
|
||||
object ScalaApp extends App {
|
||||
|
||||
val a = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
|
||||
|
||||
// 1.方式一 相当于Java中的增强for循环
|
||||
for (elem <- a) {
|
||||
print(elem)
|
||||
}
|
||||
|
||||
// 2.方式二
|
||||
for (index <- 0 until a.length) {
|
||||
print(a(index))
|
||||
}
|
||||
|
||||
// 3.方式三, 是第二种方式的简写
|
||||
for (index <- a.indices) {
|
||||
print(a(index))
|
||||
}
|
||||
|
||||
// 4.反向遍历
|
||||
for (index <- a.indices.reverse) {
|
||||
print(a(index))
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
这里我们没有将代码写在main方法中,而是继承自App.scala,这是Scala提供的一种简写方式,此时将代码写在类中,等价于写在main方法中,直接运行该类即可。
|
||||
|
||||
|
||||
|
||||
## 四、数组转换
|
||||
|
||||
数组转换是指由现有数组产生新的数组。假设当前拥有a数组,想把a中的偶数元素乘以10后产生一个新的数组,可以采用下面两种方式来实现:
|
||||
|
||||
```scala
|
||||
object ScalaApp extends App {
|
||||
|
||||
val a = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
|
||||
|
||||
// 1.方式一 yield关键字
|
||||
val ints1 = for (elem <- a if elem % 2 == 0) yield 10 * elem
|
||||
for (elem <- ints1) {
|
||||
println(elem)
|
||||
}
|
||||
|
||||
// 2.方式二 采用函数式编程的方式,这和Java 8中的函数式编程是类似的,这里采用下划线标表示其中的每个元素
|
||||
val ints2 = a.filter(_ % 2 == 0).map(_ * 10)
|
||||
for (elem <- ints1) {
|
||||
println(elem)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 五、多维数组
|
||||
|
||||
和Java中一样,多维数组由单位数组组成。
|
||||
|
||||
```scala
|
||||
object ScalaApp extends App {
|
||||
|
||||
val matrix = Array(Array(11, 12, 13, 14, 15, 16, 17, 18, 19, 20),
|
||||
Array(21, 22, 23, 24, 25, 26, 27, 28, 29, 30),
|
||||
Array(31, 32, 33, 34, 35, 36, 37, 38, 39, 40))
|
||||
|
||||
|
||||
for (elem <- matrix) {
|
||||
|
||||
for (elem <- elem) {
|
||||
print(elem + "-")
|
||||
}
|
||||
println()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
打印输出如下:
|
||||
11-12-13-14-15-16-17-18-19-20-
|
||||
21-22-23-24-25-26-27-28-29-30-
|
||||
31-32-33-34-35-36-37-38-39-40-
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 六、与Java互操作
|
||||
|
||||
由于Scala的数组是使用Java的数组来实现的,所以两者之间可以相互转换。
|
||||
|
||||
```scala
|
||||
import java.util
|
||||
|
||||
import scala.collection.mutable.ArrayBuffer
|
||||
import scala.collection.{JavaConverters, mutable}
|
||||
|
||||
object ScalaApp extends App {
|
||||
|
||||
val element = ArrayBuffer("hadoop", "spark", "storm")
|
||||
// Scala转Java
|
||||
val javaList: util.List[String] = JavaConverters.bufferAsJavaList(element)
|
||||
// Java转Scala
|
||||
val scalaBuffer: mutable.Buffer[String] = JavaConverters.asScalaBuffer(javaList)
|
||||
for (elem <- scalaBuffer) {
|
||||
println(elem)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 参考资料
|
||||
|
||||
1. Martin Odersky(著),高宇翔(译) . Scala编程(第3版)[M] . 电子工业出版社 . 2018-1-1
|
||||
2. 凯.S.霍斯特曼(著),高宇翔(译) . 快学Scala(第2版)[M] . 电子工业出版社 . 2017-7
|
||||
## 一、定长数组
|
||||
|
||||
在Scala中,如果你需要一个长度不变的数组,可以用Array。
|
||||
|
||||
```scala
|
||||
// 10个整数的数组,所有元素初始化为0
|
||||
scala> val nums=new Array[Int](10)
|
||||
nums: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
|
||||
|
||||
// 10个元素的字符串数组,所有元素初始化为null
|
||||
scala> val strings=new Array[String](10)
|
||||
strings: Array[String] = Array(null, null, null, null, null, null, null, null, null, null)
|
||||
|
||||
// 使用指定值初始化,此时不需要new关键字
|
||||
scala> val a=Array("hello","scala")
|
||||
a: Array[String] = Array(hello, scala)
|
||||
|
||||
// 使用()来访问元素
|
||||
scala> a(0)
|
||||
res3: String = hello
|
||||
```
|
||||
|
||||
这里需要注意以下两点:
|
||||
|
||||
+ 在scala中使用`(index)`而不是`[index]`来访问数组中的元素,因为访问元素,对于Scala来说是方法调用,`(index)`相当于执行了`.apply(index)`方法。
|
||||
+ scala中的数组与Java中的是等价的,`Array[Int]()`在虚拟机层面就等价于Java的`int[]`。
|
||||
|
||||
|
||||
|
||||
## 二、变长数组
|
||||
|
||||
在scala中,通过ArrayBuffer实现变长数组(又称缓冲数组),相当于Java中的ArrayList。变长数组的构建方式及常用操作如下:
|
||||
|
||||
```java
|
||||
import scala.collection.mutable.ArrayBuffer
|
||||
|
||||
object ScalaApp {
|
||||
|
||||
// 相当于Java中的main方法
|
||||
def main(args: Array[String]): Unit = {
|
||||
// 1.声明变长数组(缓冲数组)
|
||||
val ab = new ArrayBuffer[Int]()
|
||||
// 2.在末端增加元素
|
||||
ab += 1
|
||||
// 3.在末端添加多个元素
|
||||
ab += (2, 3, 4)
|
||||
// 4.可以使用++=追加任何集合
|
||||
ab ++= Array(5, 6, 7)
|
||||
// 5.缓冲数组可以直接打印查看
|
||||
println(ab)
|
||||
// 6.移除最后三个元素
|
||||
ab.trimEnd(3)
|
||||
// 7.在第1个元素之后插入多个新元素
|
||||
ab.insert(1, 8, 9)
|
||||
// 8.从第2个元素开始,移除3个元素,不指定第二个参数的话,默认值为1
|
||||
ab.remove(2, 3)
|
||||
// 9.缓冲数组转定长数组
|
||||
val abToA = ab.toArray
|
||||
// 10. 定长数组打印为其hashcode值
|
||||
println(abToA)
|
||||
// 11. 定长数组转缓冲数组
|
||||
val aToAb = abToA.toBuffer
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
这里需要说明的是:由于scala的变长数组是使用java的集合来实现的,而Java集合的底层实现是链表,这意味使用`+= `在末尾插入元素是一个高效的操作,因为其时间复杂度是O(1)。而使用insert随机插入元素的时间复杂度是O(n),因为在其插入位置之后的所有元素都要进行对应地后移,所以在`ArrayBuffer`中随机插入元素是一个低效的操作。
|
||||
|
||||
## 三、数组遍历
|
||||
|
||||
```scala
|
||||
object ScalaApp extends App {
|
||||
|
||||
val a = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
|
||||
|
||||
// 1.方式一 相当于Java中的增强for循环
|
||||
for (elem <- a) {
|
||||
print(elem)
|
||||
}
|
||||
|
||||
// 2.方式二
|
||||
for (index <- 0 until a.length) {
|
||||
print(a(index))
|
||||
}
|
||||
|
||||
// 3.方式三, 是第二种方式的简写
|
||||
for (index <- a.indices) {
|
||||
print(a(index))
|
||||
}
|
||||
|
||||
// 4.反向遍历
|
||||
for (index <- a.indices.reverse) {
|
||||
print(a(index))
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
这里我们没有将代码写在main方法中,而是继承自App.scala,这是Scala提供的一种简写方式,此时将代码写在类中,等价于写在main方法中,直接运行该类即可。
|
||||
|
||||
|
||||
|
||||
## 四、数组转换
|
||||
|
||||
数组转换是指由现有数组产生新的数组。假设当前拥有a数组,想把a中的偶数元素乘以10后产生一个新的数组,可以采用下面两种方式来实现:
|
||||
|
||||
```scala
|
||||
object ScalaApp extends App {
|
||||
|
||||
val a = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
|
||||
|
||||
// 1.方式一 yield关键字
|
||||
val ints1 = for (elem <- a if elem % 2 == 0) yield 10 * elem
|
||||
for (elem <- ints1) {
|
||||
println(elem)
|
||||
}
|
||||
|
||||
// 2.方式二 采用函数式编程的方式,这和Java 8中的函数式编程是类似的,这里采用下划线标表示其中的每个元素
|
||||
val ints2 = a.filter(_ % 2 == 0).map(_ * 10)
|
||||
for (elem <- ints1) {
|
||||
println(elem)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 五、多维数组
|
||||
|
||||
和Java中一样,多维数组由单位数组组成。
|
||||
|
||||
```scala
|
||||
object ScalaApp extends App {
|
||||
|
||||
val matrix = Array(Array(11, 12, 13, 14, 15, 16, 17, 18, 19, 20),
|
||||
Array(21, 22, 23, 24, 25, 26, 27, 28, 29, 30),
|
||||
Array(31, 32, 33, 34, 35, 36, 37, 38, 39, 40))
|
||||
|
||||
|
||||
for (elem <- matrix) {
|
||||
|
||||
for (elem <- elem) {
|
||||
print(elem + "-")
|
||||
}
|
||||
println()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
打印输出如下:
|
||||
11-12-13-14-15-16-17-18-19-20-
|
||||
21-22-23-24-25-26-27-28-29-30-
|
||||
31-32-33-34-35-36-37-38-39-40-
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 六、与Java互操作
|
||||
|
||||
由于Scala的数组是使用Java的数组来实现的,所以两者之间可以相互转换。
|
||||
|
||||
```scala
|
||||
import java.util
|
||||
|
||||
import scala.collection.mutable.ArrayBuffer
|
||||
import scala.collection.{JavaConverters, mutable}
|
||||
|
||||
object ScalaApp extends App {
|
||||
|
||||
val element = ArrayBuffer("hadoop", "spark", "storm")
|
||||
// Scala转Java
|
||||
val javaList: util.List[String] = JavaConverters.bufferAsJavaList(element)
|
||||
// Java转Scala
|
||||
val scalaBuffer: mutable.Buffer[String] = JavaConverters.asScalaBuffer(javaList)
|
||||
for (elem <- scalaBuffer) {
|
||||
println(elem)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 参考资料
|
||||
|
||||
1. Martin Odersky . Scala编程(第3版)[M] . 电子工业出版社 . 2018-1-1
|
||||
2. 凯.S.霍斯特曼 . 快学Scala(第2版)[M] . 电子工业出版社 . 2017-7
|
@ -0,0 +1,272 @@
|
||||
# Scala映射和元组
|
||||
|
||||
## 一、映射(Map)
|
||||
|
||||
### 1.1 构造映射
|
||||
|
||||
```scala
|
||||
scala> import scala.collection.immutable.HashMap
|
||||
import scala.collection.immutable.HashMap
|
||||
|
||||
// 初始化一个空map
|
||||
scala> val scores01 = new HashMap[String, Int]
|
||||
scores01: scala.collection.immutable.HashMap[String,Int] = Map()
|
||||
|
||||
// 从指定的值初始化映射(方式一)
|
||||
scala> val scores02 = Map("hadoop" -> 10, "spark" -> 20, "storm" -> 30)
|
||||
scores02: scala.collection.immutable.Map[String,Int] = Map(hadoop -> 10, spark -> 20, storm -> 30)
|
||||
|
||||
// 从指定的值初始化映射(方式二)
|
||||
scala> val scores03 = Map(("hadoop", 10), ("spark", 20), ("storm", 30))
|
||||
scores03: scala.collection.immutable.Map[String,Int] = Map(hadoop -> 10, spark -> 20, storm -> 30)
|
||||
```
|
||||
|
||||
采用上面方式得到的都是不可变(immutable)映射,想要得到可变映射,则用:
|
||||
|
||||
```scala
|
||||
scala> val scores04 = scala.collection.mutable.Map("hadoop" -> 10, "spark" -> 20, "storm" -> 30)
|
||||
scores04: scala.collection.mutable.Map[String,Int] = Map(spark -> 20, hadoop -> 10, storm -> 30)
|
||||
```
|
||||
|
||||
### 1.2 获取值
|
||||
|
||||
```scala
|
||||
object ScalaApp extends App {
|
||||
|
||||
val scores = Map("hadoop" -> 10, "spark" -> 20, "storm" -> 30)
|
||||
|
||||
// 1.获取指定key对应的值
|
||||
println(scores("hadoop"))
|
||||
|
||||
// 2. 如果对应的值不存在则使用默认值
|
||||
println(scores.getOrElse("hadoop01", 100))
|
||||
}
|
||||
```
|
||||
|
||||
### 1.3 新增/修改/删除值
|
||||
|
||||
可变映射允许进行新增、修改、删除等操作。
|
||||
|
||||
```scala
|
||||
object ScalaApp extends App {
|
||||
|
||||
val scores = scala.collection.mutable.Map("hadoop" -> 10, "spark" -> 20, "storm" -> 30)
|
||||
|
||||
// 1.如果key存在则更新
|
||||
scores("hadoop") = 100
|
||||
|
||||
// 2.如果key不存在则新增
|
||||
scores("flink") = 40
|
||||
|
||||
// 3.可以通过+=来进行多个更新或新增操作
|
||||
scores += ("spark" -> 200, "hive" -> 50)
|
||||
|
||||
// 4.可以通过-= 来移除某个键和值
|
||||
scores -= "storm"
|
||||
|
||||
for (elem <- scores) {println(elem)}
|
||||
}
|
||||
|
||||
// 输出内容如下
|
||||
(spark,200)
|
||||
(hadoop,100)
|
||||
(flink,40)
|
||||
(hive,50)
|
||||
```
|
||||
|
||||
不可变映射不允许进行新增、修改、删除等操作,但是允许由不可变映射产生新的映射。
|
||||
|
||||
```scala
|
||||
object ScalaApp extends App {
|
||||
|
||||
val scores = Map("hadoop" -> 10, "spark" -> 20, "storm" -> 30)
|
||||
|
||||
val newScores = scores + ("spark" -> 200, "hive" -> 50)
|
||||
|
||||
for (elem <- scores) {println(elem)}
|
||||
|
||||
}
|
||||
|
||||
// 输出内容如下
|
||||
(hadoop,10)
|
||||
(spark,200)
|
||||
(storm,30)
|
||||
(hive,50)
|
||||
```
|
||||
|
||||
### 1.4 遍历映射
|
||||
|
||||
```java
|
||||
object ScalaApp extends App {
|
||||
|
||||
val scores = Map("hadoop" -> 10, "spark" -> 20, "storm" -> 30)
|
||||
|
||||
// 1. 遍历键
|
||||
for (key <- scores.keys) { println(key) }
|
||||
|
||||
// 2. 遍历值
|
||||
for (value <- scores.values) { println(value) }
|
||||
|
||||
// 3. 遍历键值对
|
||||
for ((key, value) <- scores) { println(key + ":" + value) }
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
### 1.5 产生新映射
|
||||
|
||||
可以使用`yield`关键字从现有映射产生新的映射。
|
||||
|
||||
```scala
|
||||
object ScalaApp extends App {
|
||||
|
||||
val scores = Map("hadoop" -> 10, "spark" -> 20, "storm" -> 30)
|
||||
|
||||
// 1.将scores中所有的值扩大10倍
|
||||
val newScore = for ((key, value) <- scores) yield (key, value * 10)
|
||||
for (elem <- newScore) { println(elem) }
|
||||
|
||||
|
||||
// 2.将键和值互相调换
|
||||
val reversalScore: Map[Int, String] = for ((key, value) <- scores) yield (value, key)
|
||||
for (elem <- reversalScore) { println(elem) }
|
||||
|
||||
}
|
||||
|
||||
// 输出
|
||||
(hadoop,100)
|
||||
(spark,200)
|
||||
(storm,300)
|
||||
|
||||
(10,hadoop)
|
||||
(20,spark)
|
||||
(30,storm)
|
||||
```
|
||||
|
||||
### 1.6 有序映射
|
||||
|
||||
在使用Map时候,如果不指定,默认使用的是HashMap,如果想要使用`TreeMap`或者`LinkedHashMap`,则需要显式的指定。
|
||||
|
||||
```scala
|
||||
object ScalaApp extends App {
|
||||
|
||||
// 1.使用TreeMap,按照键的字典序进行排序
|
||||
val scores01 = scala.collection.mutable.TreeMap("B" -> 20, "A" -> 10, "C" -> 30)
|
||||
for (elem <- scores01) {println(elem)}
|
||||
|
||||
// 2.使用LinkedHashMap,按照键值对的插入顺序进行排序
|
||||
val scores02 = scala.collection.mutable.LinkedHashMap("B" -> 20, "A" -> 10, "C" -> 30)
|
||||
for (elem <- scores02) {println(elem)}
|
||||
}
|
||||
|
||||
// 输出
|
||||
(A,10)
|
||||
(B,20)
|
||||
(C,30)
|
||||
|
||||
(B,20)
|
||||
(A,10)
|
||||
(C,30)
|
||||
```
|
||||
|
||||
### 1.7 其他方法
|
||||
|
||||
```scala
|
||||
object ScalaApp extends App {
|
||||
|
||||
val scores = scala.collection.mutable.TreeMap("B" -> 20, "A" -> 10, "C" -> 30)
|
||||
|
||||
// 1. 获取长度
|
||||
println(scores.size)
|
||||
|
||||
// 2. 判断是否为空
|
||||
println(scores.isEmpty)
|
||||
|
||||
// 3. 判断是否包含特定的key
|
||||
println(scores.contains("A"))
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
### 1.8 与Java互操作
|
||||
|
||||
```scala
|
||||
import java.util
|
||||
import scala.collection.{JavaConverters, mutable}
|
||||
|
||||
object ScalaApp extends App {
|
||||
|
||||
val scores = Map("hadoop" -> 10, "spark" -> 20, "storm" -> 30)
|
||||
|
||||
// scala map转java map
|
||||
val javaMap: util.Map[String, Int] = JavaConverters.mapAsJavaMap(scores)
|
||||
|
||||
// java map转scala map
|
||||
val scalaMap: mutable.Map[String, Int] = JavaConverters.mapAsScalaMap(javaMap)
|
||||
|
||||
for (elem <- scalaMap) {println(elem)}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 二、元组(Tuple)
|
||||
|
||||
元组与数组类似,但是数组中所有的元素必须是同一种类型,而元组则可以包含不同类型的元素。
|
||||
|
||||
```scala
|
||||
scala> val tuple=(1,3.24f,"scala")
|
||||
tuple: (Int, Float, String) = (1,3.24,scala)
|
||||
```
|
||||
|
||||
### 2.1 模式匹配
|
||||
|
||||
可以通过模式匹配来进行获取元组中的值并赋予对应的变量:
|
||||
|
||||
```scala
|
||||
scala> val (a,b,c)=tuple
|
||||
a: Int = 1
|
||||
b: Float = 3.24
|
||||
c: String = scala
|
||||
```
|
||||
|
||||
如果某些位置不需要赋值,则可以使用下划线代替:
|
||||
|
||||
```scala
|
||||
scala> val (a,_,_)=tuple
|
||||
a: Int = 1
|
||||
```
|
||||
|
||||
### 2.2 Zip方法
|
||||
|
||||
```scala
|
||||
object ScalaApp extends App {
|
||||
|
||||
val array01 = Array("hadoop", "spark", "storm")
|
||||
val array02 = Array(10, 20, 30)
|
||||
|
||||
// 1.zip方法得到的是多个tuple组成的数组
|
||||
val tuples: Array[(String, Int)] = array01.zip(array02)
|
||||
// 2.也可以在zip后调用toMap方法转换为映射
|
||||
val map: Map[String, Int] = array01.zip(array02).toMap
|
||||
|
||||
for (elem <- tuples) { println(elem) }
|
||||
for (elem <- map) {println(elem)}
|
||||
}
|
||||
|
||||
// 输出
|
||||
(hadoop,10)
|
||||
(spark,20)
|
||||
(storm,30)
|
||||
|
||||
(hadoop,10)
|
||||
(spark,20)
|
||||
(storm,30)
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 参考资料
|
||||
|
||||
1. Martin Odersky . Scala编程(第3版)[M] . 电子工业出版社 . 2018-1-1
|
||||
2. 凯.S.霍斯特曼 . 快学Scala(第2版)[M] . 电子工业出版社 . 2017-7
|
Loading…
x
Reference in New Issue
Block a user