目录
scala outline
scala list.map的使用
def main(args: Array[String]): Unit = {
val list: List[(String, Int)] = List(("a", 1), ("b", 2), ("c", 3))
// 统计wordcount
val newList: List[(String, Int)] = list.map(t => {
(t._1, t._2 * 2)
})
println(newList) // 输出 List((a,2), (b,4), (c,6))
}
- list.map() 这个函数要求传进去一个参数,这个参数是什么类型的呢?
要求传进去的参数是函数
,这个函数的参数是元组,即(string, int),返回类型为B,B取决于你把数据处理成什么结构
以上完整的写法为
val newList: List[(String, Int)] = list.map((t: (String, Int)) => {
(t._1, t._2 * 2)
})
- 怎么简化的?
参数为2个元组的
函数返回值类型为:string
函数返回值类型为:unit
函数返回值类型为:函数
函数返回值类型为:函数
函数返回值类型为:函数
参数为:函数
参数为:函数
参数为:代码块
总结
不管返回值类型是函数,还是参数传入需要函数,都首先要判断有无=>
,有的话,99%都是函数,进一步确定需要看=>
前面有无基本类型,例如 x:int=>; x:(int,int)=>; ()=>; 都是函数的标志