Scala编程入门---函数式编程之集合操作

时间:2022-04-05 06:44:34

集合的函数式编程:

实战常用:

//map案例实战:为List中的每个元素都添加一个前缀。
List("leo","Jen","peter","jack").map("name is"+ _) #结果: list2("name is leo","name is Jen" ,"name is peter","name is jack") //faltMap案例实战:将List中的多行句子拆分成单词
list("Hello world" ,"Hello me" ,"Hello You") #结果:list("Hello","world","Hello","me","Hello","You") //foreach案例实战:打印list中的每个单词
list("i","have","a","beautiful","house").foreach(println(_))
#结果:i have a beautiful house //zip案例实战:对学生姓名和学生成绩进行关联
list("leo","jen","Peter","Jack").zip(100,90,85,70)
#结果:list((leo,100),(jen,90),(Peter,85),(Jack,70))