集合
数组
定长数组
scala> val arr = new Array[String](10)
arr: Array[String] = Array(null, null, null, null, null, null, null, null, null, null)
scala> val arr = new Array[Int](10)
arr: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
scala> val arr = Array("a","b")
arr: Array[String] = Array(a, b)
// 指定初始化数组的数据
scala> val arr = Array.fill(5)(3.14)
arr: Array[Double] = Array(3.14, 3.14, 3.14, 3.14, 3.14)
变长数组
scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer
scala> val arrbf = ArrayBuffer[Int]()
arrbf: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
符号使用
scala> arrbf += 1
res22: arrbf.type = ArrayBuffer(1)
scala> arrbf ++= Array(2,3,4)
res24: arrbf.type = ArrayBuffer(1, 2, 3, 4)
scala> arrbf += (5,6,7)
res25: arrbf.type = ArrayBuffer(1, 2, 3, 4, 5, 6, 7)
方法使用
// 尾部追加一个元素
scala> arrbf.append(8)
scala> arrbf
res27: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8)
// 指定下标后插入元素
scala> arrbf.insert(1,9)
scala> arrbf
res29: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 9, 2, 3, 4, 5, 6, 7, 8)
scala> arrbf.insert(1,10,11)
scala> arrbf
res31: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 10, 11, 9, 2, 3, 4, 5, 6, 7, 8)
// 删除尾部3个元素
scala> arrbf.trimEnd(3)
scala> arrbf
res33: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 10, 11, 9, 2, 3, 4, 5)
// 移除index 0 后的 2个元素
scala> arrbf.remove(0,2)
scala> arrbf
res35: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(11, 9, 2, 3, 4, 5)
// 修改index 0 的值为 1
scala> arrbf(0) = 1
scala> arrbf
res37: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 9, 2, 3, 4, 5)
多维数组
scala> val arrs = new Array[Array[Int]](5)
arrs: Array[Array[Int]] = Array(null, null, null, null, null)
scala> val arrs = Array.ofDim[Int](2,3)
arrs: Array[Array[Int]] = Array(Array(0, 0, 0), Array(0, 0, 0))
scala> val arrbfs = new ArrayBuffer[ArrayBuffer[Int]]()
arrbfs: scala.collection.mutable.ArrayBuffer[scala.collection.mutable.ArrayBuffer[Int]] = ArrayBuffer()
方法
数组合并
scala> val arr1 = Array(1 to 3:_*)
arr1: Array[Int] = Array(1, 2, 3)
scala> val arr2 = Array(2 to 4:_*)
arr2: Array[Int] = Array(2, 3, 4)
// union 并集 arr1 - arr2
scala> arr1 union arr2
res16: Array[Int] = Array(1, 2, 3, 2, 3, 4)
// diff 差集 arr1 - arr2
scala> arr1 diff arr2
res17: Array[Int] = Array(1)
// intersect 交集 arr1 ^ arr2
scala> arr1 intersect arr2
res19: Array[Int] = Array(2, 3)
map
scala> val arr = Array(1 to 10:_*)
arr: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
// map 对每一个元素应用函数
scala> arr.map(_ + 1)
res38: Array[Int] = Array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
reduce
// reduce 对数组里面的元素累加求和
scala> arr.reduce((befer,after) => befer + after)
res39: Int = 55
// 从尾向前进行累 -
scala> Array(1, 2, 3, 4).reduceRight(_ - _)
res14: Int = -2
// 3 - 4 = -1
// 2 - -1 = 3
// 1 - 3 = -2
length
// length 数组长度
scala> Array(1, 2, 3, 4).length
res41: Int = 4
updated
// updated 修改指定下标的值
scala> Array(1,2,3).updated(1,1)
res17: Array[Int] = Array(1, 1, 3)
filter
// filter 筛选符合条件的元素
scala> arr.filter( _ % 2 == 0)
res40: Array[Int] = Array(2, 4, 6, 8, 10)
sum
// sum 求和
scala> arr.sum
res42: Int = 55
max min
// max 数组最大值
scala> arr.max
res43: Int = 10
// min 数组最小值
scala> arr.min
res44: Int = 1
distinct
// distinct 数据去重
scala> Array(1, 2, 3, 4, 5, 2, 3, 4, 5, 6, 7, 8).distinct
res23: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)
reverse
// reverse 反转数组元素
scala> arr.reverse
res0: Array[Int] = Array(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
count
// count 统计符合条件元素的数量
scala> Array(1, 2, 3, 4).count( _ % 2 == 0)
res26: Int = 2
sort
// sortWith 排序
scala> arr.sortWith( _ < _)
res45: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> arr.sortWith( _ > _)
res46: Array[Int] = Array(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
// sortBy 按照元组的第2个元素来排序,升序
scala> Array((1,2),(0,1),(3,4)).sortBy(_._2)
res48: Array[(Int, Int)] = Array((0,1), (1,2), (3,4))
zip
// zip 将两个数组的数据合并,数据形式(k,v)
scala> val arr1 = Array("a","b","c")
arr1: Array[String] = Array(a, b, c)
scala> val arr2 = Array(1,2,3)
arr2: Array[Int] = Array(1, 2, 3)
scala> arr1.zip(arr2)
res1: Array[(String, Int)] = Array((a,1), (b,2), (c,3))
mkString
// mkString 指定元素之间的分隔符,组成一个字符串。
scala> arr.mkString("--")
res50: String = 1--2--3--4--5--6--7--8--9--10
// 指定将数组组成字符串的 前缀,分隔符,后缀
scala> arr.mkString("<","--",">")
res51: String = <1--2--3--4--5--6--7--8--9--10>
toXxx
// toBuffer 使Array 转为 ArrayBuffer
scala> arr.toBuffer
res54: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
// toArray 使ArrayBuffer 转为 Array
scala> arr.toBuffer.toArray
res55: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
flatten
// flatten 展平数据
scala> Array(Array(1 to 3:_*),Array(4 to 6:_*)).flatten
res9: Array[Int] = Array(1, 2, 3, 4, 5, 6)
flatMap
// flatMap 先对外层应用 map 然后再对外层应用 flatten
scala> Array(Array(1 to 3:_*),Array(4 to 6:_*)).flatMap(_.map(_ * 2))
res11: Array[Int] = Array(2, 4, 6, 8, 10, 12)
group
scala> val arrs = Array("one 1","two 2","three 3","one 2","two 1").map(_.split(" "))
arrs: Array[Array[String]] = Array(Array(one, 1), Array(two, 2), Array(three, 3), Array(one, 2), Array(two, 1))
scala> arrs.groupBy(_(0))
res0: scala.collection.immutable.Map[String,Array[Array[String]]] = Map(one -> Array(Array(one, 1), Array(one, 2)), three -> Array(Array(three, 3)), two -> Arr
ay(Array(two, 2), Array(two, 1)))
partition,splitAt
// partition 将数据分成两部分,(符号条件,不符合条件)
scala> Array(1 to 10:_*).partition(_ % 2 == 0)
res0: (Array[Int], Array[Int]) = (Array(2, 4, 6, 8, 10),Array(1, 3, 5, 7, 9))
// splitAt 将数据分成两部分,(前n个,剩下的)
scala> Array(1 to 10:_*).splitAt(3)
res1: (Array[Int], Array[Int]) = (Array(1, 2, 3),Array(4, 5, 6, 7, 8, 9, 10))
scan
// scan 从首开始给定初始值0,以reduce的方式计算,并将计算过程的结果生成集合返回
scala> Array(1,2,3).scan(0)(_ + _)
res2: Array[Int] = Array(0, 1, 3, 6)
// 从尾开始
scala> Array(1,2,3).scanRight(0)(_ + _)
res3: Array[Int] = Array(6, 5, 3, 0)
take,slice
// take 取前3个数据
scala> Array(1,2,3,4,5,6,7).take(3)
res4: Array[Int] = Array(1, 2, 3)
// 取后3个数据
scala> Array(1,2,3,4,5,6,7).takeRight(3)
res5: Array[Int] = Array(5, 6, 7)
// slice 取下标为[0,4)的数据
scala> Array(1,2,3,4,5,6,7).slice(0,4)
res9: Array[Int] = Array(1, 2, 3, 4)
forall
// forall 判断元素是否都 小于7
scala> Array(1,2,3,4,5,6,7).forall( _ < 7)
res11: Boolean = false
combinations,permutations
// combinations 返回取2个元素进行排列组合所有可能的种类
scala> Array(1,2,3).combinations(2).toList
res13: List[Array[Int]] = List(Array(1, 2), Array(1, 3), Array(2, 3))
// permutations 返回所有元素排列组合的所有种类
scala> Array(1,2,3).permutations.toList
res15: List[Array[Int]] = List(Array(1, 2, 3), Array(1, 3, 2), Array(2, 1, 3), Array(2, 3, 1), Array(3, 1, 2), Array(3, 2, 1))
// 按照指定要求生成数据;
// 第一个参数:每个list的元素个数为3,第二个参数:选取规则与上一个listindex相差2
// 1 2 3 4 5 6 7 8 9 10
// list0 1 2 3
// listt1 3 4 5
scala> List(1 to 10:_*).sliding(3,2).toList
res22: List[List[Int]] = List(List(1, 2, 3), List(3, 4, 5), List(5, 6, 7), List(7, 8, 9), List(9, 10))
// 参数1和参数2相同的情况
scala> List(1 to 10:_*).grouped(3).toList
res24: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 6), List(7, 8, 9), List(10
// 自定义 case class
scala> case class Book(name: String,num: Int)
defined class Book
scala> val book = Array(Book("语文", 12),Book("数学", 13),Book("英语", 15))
book: Array[Book] = Array(Book(语文,12), Book(数学,13), Book(英语,15))
// 取num的最大值
scala> book.maxBy(_.num)
res1: Book = Book(英语,15)
// 取num的最小值
scala> book.minBy(_.num)
res2: Book = Book(语文,12)
// 按照num 升序排序
scala> book.sortBy(_.num)
res3: Array[Book] = Array(Book(语文,12), Book(数学,13), Book(英语,15))
scala> book.sortWith((book1,book2) => book1.num < book2.num)
res6: Array[Book] = Array(Book(语文,12), Book(数学,13), Book(英语,15))
scala集合和java集合相互转换
// 导入scala转java的隐式转换包。
scala> import scala.collection.JavaConverters._
import scala.collection.JavaConverters._
// scala List 对应 java List
scala> val javaList = List(1 to 5:_*).asJava
javaList: java.util.List[Int] = [1, 2, 3, 4, 5]
// java List 对应 scala Buffer
scala> val scalaBuffer = javaList.asScala
scalaBuffer: scala.collection.mutable.Buffer[Int] = Buffer(1, 2, 3, 4, 5)
其他类型转换:https://blog.csdn.net/u013013024/article/details/80452019
List
增
scala> val left = List(1 to 5:_*)
left: List[Int] = List(1, 2, 3, 4, 5)
scala> val right = List(6 to 10:_*)
right: List[Int] = List(6, 7, 8, 9, 10)
// 连接两个list
scala> left ++ right
res0: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> left ::: (right)
res5: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
// 调换方向连接两个list
scala> left.++:(right)
res4: List[Int] = List(6, 7, 8, 9, 10, 1, 2, 3, 4, 5)
scala> left.:::(right)
res9: List[Int] = List(6, 7, 8, 9, 10, 1, 2, 3, 4, 5)
// 尾部添加元素
scala> left :+ 11
res12: List[Int] = List(1, 2, 3, 4, 5, 11)
// 头部添加元素
scala> left.+:(11)
res15: List[Int] = List(11, 1, 2, 3, 4, 5)
scala> left.::(11)
res17: List[Int] = List(11, 1, 2, 3, 4, 5)
删
scala> val list = List(3 to 10:_*)
list: List[Int] = List(3, 4, 5, 6, 7, 8, 9, 10)
scala> val list1 = List(3,4,5,1,2)
list1: List[Int] = List(3, 4, 5, 1, 2)
// 从首部删除n个数据
scala> list.drop(4)
res18: List[Int] = List(7, 8, 9, 10)
// 从尾部删除n个数据
scala> list.dropRight(3)
res20: List[Int] = List(3, 4, 5, 6, 7)
// 从左往右匹配,直到匹配到不符合条件的数据,保留后面的数据
scala> list.dropWhile(_ < 6)
res25: List[Int] = List(6, 7, 8, 9, 10)
scala> l.dropWhile(_ < 4)
res27: List[Int] = List(4, 5, 1, 2)
修改查询
scala> val list = List(2 to 6:_*)
list: List[Int] = List(2, 3, 4, 5, 6)
// 通过下标获取数据
scala> list(0)
res28: Int = 2
// 默认不可修改
scala> List
res29: scala.collection.immutable.List.type = scala.collection.immutable.List$@2fc2bb2f
scala> list(0) = 0
<console>:13: error: value update is not a member of List[Int]
list(0) = 0
常用方法
scala> val list = List(3 to 10:_*)
list: List[Int] = List(3, 4, 5, 6, 7, 8, 9, 10)
// 获取前n个数据
scala> list.take(3)
res31: List[Int] = List(3, 4, 5)
// 条件获取
scala> list.takeWhile( _ < 5)
res36: List[Int] = List(3, 4)
scala> list.takeWhile( _ > 5)
res37: List[Int] = List()
// 元素转换为字符
scala> list.mkString
res38: String = 345678910
// 统计符合条件的数据的数量
scala> list.count( _ % 2 == 0)
res39: Int = 4
// 累加计算
scala> list.sum
res42: Int = 52
scala> val lis = "a" :: ("b" :: ("c" :: Nil))
lis: List[String] = List(a, b, c)
scala> val list = List("One Line","Two line","Three line")
list: List[String] = List(One Line, Two line, Three line)
scala> list.map(_.split(" "))
res49: List[Array[String]] = List(Array(One, Line), Array(Two, line), Array(Three, line))
scala> list.flatMap(_.split(" "))
res51: List[String] = List(One, Line, Two, line, Three, line)
scala> list.map(_.toUpperCase)
res50: List[String] = List(ONE LINE, TWO LINE, THREE LINE)
Set集合
Set
- 元素不可重复
- 插入的数据无序
// 添加元素
scala> Set() + 1
res44: scala.collection.immutable.Set[Int] = Set(1)
可变Set
scala> import scala.collection.mutable.Set
import scala.collection.mutable.Set
增
scala> val set = scala.collection.mutable.Set[Int]()
set: scala.collection.mutable.Set[Int] = Set()
// 添加数据
scala> set.add(1)
res2: Boolean = true
scala> set
res3: scala.collection.mutable.Set[Int] = Set(1)
scala> set += 2
res4: set.type = Set(1, 2)
scala> set
res5: scala.collection.mutable.Set[Int] = Set(1, 2)
// 集合连接
scala> val set1 = scala.collection.mutable.Set(1 to 10:_*)
set1: scala.collection.mutable.Set[Int] = Set(9, 1, 5, 2, 6, 3, 10, 7, 4, 8)
scala> val muSet = set ++ set1
muSet: scala.collection.mutable.Set[Int] = Set(9, 1, 5, 2, 6, 3, 10, 7, 4, 8)
删
// 删除元素 1
scala> muSet.remove(1)
res14: Boolean = true
scala> muSet
res15: scala.collection.mutable.Set[Int] = Set(9, 5, 2, 6, 3, 10, 7, 4, 8)
// 删除元素 9
scala> set1 -= 9
res23: scala.collection.mutable.Set[Int] = Set(1, 5, 2, 6, 3, 10, 7, 4, 8)
查
// 查看set类型
scala> set.getClass.getName()
res8: String = scala.collection.mutable.HashSet
// 获取第一个数据
scala> muSet.head
res18: Int = 9
// 获取除第一个以外的数据
scala> muSet.tail
res19: scala.collection.mutable.Set[Int] = Set(5, 2, 6, 3, 10, 7, 4, 8)
逻辑操作
scala> val muSet1 = Set(1 to 5:_*)
muSet1: scala.collection.mutable.Set[Int] = Set(1, 5, 2, 3, 4)
scala> val muSet2 = Set(3 to 7:_*)
muSet2: scala.collection.mutable.Set[Int] = Set(5, 6, 3, 7, 4)
// 取集合交集
scala> muSet1 & muSet2
res2: scala.collection.mutable.Set[Int] = Set(5, 3, 4)
// 取集合差集
scala> muSet1 &~ muSet2
res3: scala.collection.mutable.Set[Int] = Set(1, 2)
其他
// 判断元素是否存在
scala> muSet.contains(2)
res25: Boolean = true
LinkedHashSet
// LinkedHashSet 可记住插入的顺序
scala> var set = scala.collection.mutable.LinkedHashSet() + 1
set: scala.collection.mutable.LinkedHashSet[Int] = Set(1)
scala> set + 2
res46: scala.collection.mutable.LinkedHashSet[Int] = Set(1, 2)
SortedSet
// SortedSet 插入的数据会自动升序排序
scala> scala.collection.immutable.SortedSet(3,4,1,2,5,0)
res48: scala.collection.immutable.SortedSet[Int] = TreeSet(0, 1, 2, 3, 4, 5)
自定义排序
def main(args: Array[String]): Unit = {
// 默认使用 Person里面compare方法的比较规则
val set = SortedSet[Person]()
// 使用重写Ordering的compare的规则
// var set = SortedSet[Person]()(new Ordering[Person] {
// override def compare(x: Person, y: Person): Int = {
// var diff = x.getName.compareTo(y.getName) // 默认按照name升序排序
// if (diff == 0) { // name相同时按照 age 升序排序
// diff = x.getAge.compareTo(y.getAge)
// }
// diff
// }
// })
set += new Person("a", 19)
set += new Person("b", 21)
set += new Person("c", 19)
set += new Person("e", 19)
set += new Person("c", 17)
set += new Person("d", 20)
set.foreach(println)
}
class Person extends Ordered[Person] {
private var name: String = _
private var age: Int = _
def this(name: String, age: Int) {
this()
this.name = name
this.age = age
}
def getName = name
def getAge = age
override def toString: String = {
s"$name,$age"
}
override def compare(that: Person): Int = {
var diff = this.age.compareTo(that.age)
if (diff == 0) {
diff = this.name.compareTo(that.name)
}
diff
}
}
Map
不可变Map,其值不能被改变
scala> val map = Map("k1" -> "value1","k2" -> "value2","k3" -> "value3")
map: scala.collection.immutable.Map[String,String] = Map(k1 -> value1, k2 -> value2, k3 -> value3)
scala> val map = Map(("k1" -> "value1"),("k2" -> "value2"),("k3" -> "value3"))
map: scala.collection.immutable.Map[String,String] = Map(k1 -> value1, k2 -> value2, k3 -> value3)
scala> "k" -> "value"
res56: (String, String) = (k,value)
scala> val map = Map(("k1","value1"),("k2","value2"),("k3","value3"))
map: scala.collection.immutable.Map[String,String] = Map(k1 -> value1, k2 -> value2, k3 -> value3)
可变Map
scala> import scala.collection.mutable.Map
import scala.collection.mutable.Map
scala> val muMap = Map[Int,String]()
muMap: scala.collection.mutable.Map[Int,String] = Map()
scala> Map("k1" -> "v1","k2" -> "v2","k3" -> "v3")
res35: scala.collection.mutable.Map[String,String] = Map(k2 -> v2, k1 -> v1, k3 -> v3)
scala> Map(("k1","v1"),("k2", "v2"),("k3","v3"))
res36: scala.collection.mutable.Map[String,String] = Map(k2 -> v2, k1 -> v1, k3 -> v3)
增
// 添加一个
scala> muMap(2) = "b"
// 添加多个
scala> muMap += ((3,"v3"),(4,"v4"))
res7: muMap.type = Map(2 -> v2, 4 -> v4, 3 -> v3)
删
// 删除key为 3 的数据
scala> muMap -= 3
res9: muMap.type = Map(2 -> v2, 4 -> v4)
改
scala> muMap(2) = "b"
查
scala> muMap.get(4)
res11: Option[String] = Some(v4)
scala> val map = scala.collection.mutable.Map(("k1","value1"),("k2","value2"),("k3","value3"))
map: scala.collection.mutable.Map[String,String] = Map(k2 -> value2, k1 -> value1, k3 -> value3)
scala> val map = scala.collection.mutable.Map[String,Int]()
map: scala.collection.mutable.Map[String,Int] = Map()
scala> val map = scala.collection.mutable.HashMap[String,Int]()
map: scala.collection.mutable.HashMap[String,Int] = Map()
SortedMap
自动按照key排序
scala> val map = scala.collection.immutable.SortedMap(("a","v1"),("c","v3"),("b","v2"))
map: scala.collection.immutable.SortedMap[String,String] = Map(a -> v1, b -> v2, c -> v3)
LinkedHashMap
记住插入的顺序
scala> val map = scala.collection.mutable.LinkedHashMap(("a","v1"),("c","v3"),("b","v2"))
map: scala.collection.mutable.LinkedHashMap[String,String] = Map(a -> v1, c -> v3, b -> v2)
方法
可变Map方法
scala> val map = scala.collection.mutable.Map(("k1","value1"),("k2","value2"),("k3","value3"))
map: scala.collection.mutable.Map[String,String] = Map(k2 -> value2, k1 -> value1, k3 -> value3)
// 通过key获取值
scala> map.get("k1")
res57: Option[String] = Some(value1)
scala> map.get("k0")
res58: Option[String] = None
// 没有key返回后面的值
scala> map.getOrElse("k0","there is no key!")
res59: String = there is no key!
// 修改值
scala> scala> map("k1") = "v1"
scala> map
res61: scala.collection.mutable.Map[String,String] = Map(k2 -> value2, k1 -> v1, k3 -> value3)
// 添加 k-v
scala> map += ("k4"-> "value4","k5" -> "value5")
res2: map.type = Map(k2 -> value2, k5 -> value5, k4 -> value4, k1 -> v1, k3 -> value3)
// 添加单个 k-v
scala> map("k5") = "value5"
// 删除 k-v
scala> map -= "k1"
res3: map.type = Map(k2 -> value2, k5 -> value5, k4 -> value4, k3 -> value3)
不可变Map方法
scala> val map = Map(("k1","value1"),("k2","value2"),("k3","value3"))
map: scala.collection.immutable.Map[String,String] = Map(k1 -> value1, k2 -> value2, k3 -> value3)
// 合并不可变map创建新map
scala> map + ("k4"-> "value4","k5" -> "value5")
res5: scala.collection.immutable.Map[String,String] = Map(k2 -> value2, k5 -> value5, k1 -> value1, k4 -> value4, k3 -> value3)
// 取删除之外的数据,创建新map
scala> map - "k1"
res6: scala.collection.immutable.Map[String,String] = Map(k2 -> value2, k3 -> value3)
// 遍历map的方式
scala> for((k,v) <- map) println(s"k=$k, v=$v")
k=k1, v=value1
k=k2, v=value2
k=k3, v=value3
scala> for((k,_) <- map) println(s"k=$k")
k=k1
k=k2
k=k3
scala> for(k <- map.keySet) println(s"k=$k")
k=k1
k=k2
k=k3
scala> for(v <- map.values) println(s"v=$v")
v=value1
v=value2
v=value3
// 反转map的 key value
scala> for((k,v) <- map) yield (v,k)
res13: scala.collection.immutable.Map[String,String] = Map(value1 -> k1, value2 -> k2, value3 -> k3)
元组
// 创建tuple
scala> val tuple = (1,3.14,"tuple")
tuple: (Int, Double, String) = (1,3.14,tuple)
scala> val tuple = new Tuple3[Int,Double,String](1,3.14,"tuple")
tuple: (Int, Double, String) = (1,3.14,tuple)
// 获取第2个元素,从1开始数
scala> tuple._2
res14: Double = 3.14
// 分别赋值
scala> val (num,doub,str) = (1,3.14,"tuple")
num: Int = 1
doub: Double = 3.14
str: String = tuple
scala> print(num,doub,str)
(1,3.14,tuple)
// 分离出大写和小写字母
scala> "One Two".partition(_.isUpper)
res16: (String, String) = (OT,ne wo)
// productIterator 将tuple转换为迭代器遍历
scala> tuple.productIterator.foreach(println)
1
3.14
tuple
Option
Option类型用来表示可能存在也可能不存在的值, Some(值), None
scala> val map = Map("a" -> 1,"b" -> 2, "c" -> 3)
map: scala.collection.immutable.Map[String,Int] = Map(a -> 1, b -> 2, c -> 3)
scala> map.get("a")
res0: Option[Int] = Some(1)
scala> val s: Option[Int] = Some(2)
s: Option[Int] = Some(2)
scala> val n: Option[Int] = None
n: Option[Int] = None
scala> n.isEmpty
res2: Boolean = true
scala> s.isEmpty
res1: Boolean = false
scala> s.getOrElse(1)
res3: Int = 2
scala> n.getOrElse(1)
res5: Int = 1