码迷,mamicode.com
首页 > 其他好文 > 详细

大数据之高阶函数应用

时间:2020-05-26 22:03:47      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:style   学生   strong   student   col   val   lis   calendar   ==   

 题目一:高阶函数应用(共计30分)

1)将下列学生存储在一个list,命名为studentsList 中5分)

2)遍历studentsList,使用高阶函数输出上述内容5分)

3)创建girlList,boyList,lt18List,gt18List (5分)

4)利用高阶函数遍历studentsList,把所有男生存入boyList (5分)

5)大于等于18岁的存入gt18List (5分)

6)小于18岁的lt18List存入lt18List中 (5分)。

 

import java.util.{Calendar, Date}

import scala.collection.mutable.ListBuffer

object Test {
def main(args: Array[String]): Unit = {
val studentList: ListBuffer[Tuple3[String, String, Int]] = ListBuffer()
studentList.+= (("张三", "男", 1998))
studentList.+= (("李四", "女", 1997))
studentList.+= (("王五", "男", 1997))
studentList.+= (("宫六", "女", 1999))
studentList.+= (("何七", "女", 1993))
studentList.+= (("周八", "男", 1992))
studentList.+= (("申九", "女", 1999))
studentList.+= (("rose", "女", 2001))
studentList.+= (("Jhon", "男", 2000))

studentList.foreach(println)

val boyList = studentList.filter(tuple => tuple._2=="男")

val year = Calendar.getInstance().get(Calendar.YEAR)
val gt18List = studentList.filter(tuple => year-tuple._3 >= 18)
val lt18List = studentList.filter(tuple => year-tuple._3 < 18)
println("--------------------boyList------------------------")
boyList.foreach(println)
println("--------------------gt18List------------------------")
gt18List.foreach(println)
println("--------------------lt18List------------------------")
lt18List.foreach(println)
}

}

大数据之高阶函数应用

标签:style   学生   strong   student   col   val   lis   calendar   ==   

原文地址:https://www.cnblogs.com/whyuan/p/12968840.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!