List
val nnums = List(1,2,3) //> nnums : List[Int] = List(1, 2, 3)
在列表上进行map操作:
nnums.map( (i : Int) => i*2) //> res0: List[Int] = List(2, 4, 6)
val nnums = List(1,2,3) //> nnums : List[Int] = List(1, 2, 3)
def timesTwo(i: Int): Int = i * 2 //> timesTwo: (i: Int)Int
nnums.map( timesTwo) //> res0: List[Int] = List(2, 4, 6)
foreach操作:
val nnums = List(1,2,3) //> nnums : List[Int] = List(1, 2, 3)
val re = nnums.foreach((i: Int) => i * 2) //> re : Unit = ()
注意到没有返回值,scala的Unit相当于Java的void
filter过滤操作:
val nnums = List(1,2,3) //> nnums : List[Int] = List(1, 2, 3)
val re = nnums.filter((i: Int) => i % 2 == 0) //> re : List[Int] = List(2)
def isEven(i: Int): Boolean = i % 2 == 0 //> isEven: (i: Int)Boolean
nnums.filter(isEven _) //> res0: List[Int] = List(2)
Set
集合,不重复:
val strs = Set("Helo","workd") //> strs : scala.collection.immutable.Set[String] = Set(Helo, workd)
注意到这种方式使用Set时是不可变的。
Tuple
元组,使用()表示,通过从1开始的下表访问
val tup = ("localhost", 80) //> tup : (String, Int) = (localhost,80)
tup._1 //> res0: String = localhost
tup._2 //> res1: Int = 80
特殊的二元组可以使用->表示:
val pair = 1 -> 2 //> pair : (Int, Int) = (1,2)
可以用于模式匹配:
val result = tup match {
case ("localhost" , 80) =>"default"
case pair =>"pair"
case _ => "others"
} //> result : String = default
Map
Map("key" -> "value")
Map("one"->1, "two"->2)
Map(("one",1), ("two",2))
Map(1 -> Map("foo" -> "bar"))
Map("timesTwo" -> { timesTwo(_) })
Option
Option用于表示可能有值,也可能没有值,避免null的繁琐。该Trait基本定义如下:
trait Option[T] {
def isDefined: Boolean
def get: T
def getOrElse(t: T): T
}
Option有2个子类Some[T]和None[T]
val numbers = Map("one" -> 1, "two" -> 2)
numbers.get("two") //> res5: Option[Int] = Some(2)
numbers.get("three") //> res6: Option[Int] = None
用于模式匹配:
val re = numbers.get("two") match {
case Some(n) => n * 2
case None => 0
} //> re : Int = 4
Comninator
除了上面提到的map foreach filter操作以外,还有一些操作:
zip
zip操作将两个List对应的元素组成pair:
List(1, 2, 3).zip(List("a", "b", "c"))
//> res0: List[(Int, String)] = List((1,a), (2,b), (3,c))
注意连续zip并不是组成三元组或者多元组:
List(1, 2, 3).zip(List("a", "b", "c")) .zip(List("one","two","three"))
//> res0: List[((Int, String), String)] = List(((1,a),one), ((2,b),two), ((3,c),
//| three))
Partion
partion根据条件判断函数将List分成两个List
val li = List(1, 2, 3) //> li : List[Int] = List(1, 2, 3)
li.partition( (i:Int) => i%2 == 0 ) //> res0: (List[Int], List[Int]) = (List(2),List(1, 3))
find
find根据条件函数返回第一个满足条件的元素:
val li = List(1, 2, 3) //> li : List[Int] = List(1, 2, 3)
li.find( (i:Int)=> i>2) //> res0: Option[Int] = Some(3)
drop & dropwhile
drop移除前n个元素,dropwhile移除满足条件的第一个元素:
val li = List(1, 2, 3,4) //> li : List[Int] = List(1, 2, 3, 4)
li.drop(2) //> res0: List[Int] = List(3, 4)
li.dropWhile( _ % 2 !=0 ) //> res1: List[Int] = List(2, 3, 4)
foldLeft
val li = List(1, 2, 3,4) //> li : List[Int] = List(1, 2, 3, 4)
li.foldLeft(0)((m:Int , n:Int) => m+n) //> res0: Int = 10
li.foldLeft(5)((m:Int , n:Int) => m+n) //> res1: Int = 15
其中第一个参数为起始值。
foldRight同left,只是开始的方向从右边开始。
flatten
flatten扁平化所有元素
List(List(1, 2), List(3, 4)).flatten //> res0: List[Int] = List(1, 2, 3, 4)
flatMap
先map再后flat:
List(List(1, 2), List(3, 4)).flatMap( li => li.map(_ * 2 ))
//> res0: List[Int] = List(2, 4, 6, 8)
注意作为参数的函数中,输入为List。相当于:
List(List(1, 2), List(3, 4)).map((x: List[Int]) => x.map(_ * 2)).flatten
//> res1: List[Int] = List(2, 4, 6, 8)