这个例子是,从每个list中,找到age最大的那个node。

class Node(vName: String, vAge: Int) {
// Entity class
var name: String = vName
var age: Int = vAge
} object TestGenerator { def main(args: Array[String]): Unit = {
test()
} def test(): Unit = {
// This test case is to find out the max age node from each node's list // First, define the node 1,2,3
val node1 = new Node("name-1", 1)
val node2 = new Node("name-2", 2)
val node3 = new Node("name-3", 3) // Second, def some List containing nodes
val list1: List[Node] = List(node1, node2, node3)
val list2: List[Node] = List(node1)
val list3: List[Node] = List(node1, node2)
val list4: List[Node] = List()
val list5: List[Node] = Nil
val list6: List[Node] = null // ==== Test Case 1 ====
// In this test, the generator excluded the Nil and List() and null, and take the "node" out of headOption" which is Option[Node]
// The returns are collected into node as Node val allList: Seq[List[Node]] = Seq(list1, list2, list3, list4, list5, list6) val result1 = for {
list: List[Node] <- allList // The type List[Node] is necessary for this situation, it can help to filter out list6 (null)
node <- list.sortWith(_.age > _.age).headOption
} yield node for (r <- result1) {
println(r.name)
} println("======================================================") // ***************************************************************************** // ==== Test Case 2 ====
// In this test, use get() function to get back the list instead of Seq[List[Node]] def get(i: Int): List[Node] = {
i match {
case 1 => list1;
case 2 => list2;
case 3 => list3;
case 4 => list4;
case 5 => list5;
case 6 => list6;
}
} // Define the array to contain the test lists
// List 1-5 will be used for this test, but list6 (null) cannot be handled in this approach
val arr = List(1, 2, 3, 4, 5) // list6 (null) cannot be handled thus only 1-5 here val result2 = for {
i <- arr
node <- get(i).sortWith(_.age > _.age).headOption
} yield node for (r <- result2) {
println(r.name)
} }
}

最新文章

  1. Socket简单使用
  2. 解决Can&#39;t connect to MySQL server on &#39;localhost&#39; (10048)
  3. Android APP使用NDK编译后的ffmpeg库出现undefined reference to &#39;posix_memalign&#39;错误
  4. 在线预览Office文件【效果类似百度文库】(转载)
  5. TestNg依赖高级用法之强制依赖与顺序依赖------TestNg依赖详解(二)
  6. 【HTML5】Application Cache应用程序缓存
  7. Karel运行环境配置
  8. CentOS 6.X更新Python2.7.x版本 安装pip
  9. iOS-OC-基础-NSDate常用方法
  10. Django 同步数据库命令syncdb,makemigrations,migrate
  11. 以太坊的crypto模块--以太坊源码学习
  12. Windows PowerShell 默认颜色
  13. RQNOJ 201 奥运大包围:LIS + 拼链成环
  14. BeautifulSoup 抓取网站url
  15. CSS深入理解学习笔记之float
  16. Python高级笔记(四) -- 多继承_方法解析顺序表MRO
  17. 总结:极光推送java服务端(1)
  18. (转载)new Thread的弊端及Java四种线程池的使用
  19. 64位版本为什么叫amd64,而不是intel64
  20. MT【166】青蛙跳

热门文章

  1. Luogu 4841 城市规划
  2. CF1073F Choosing Two Paths
  3. WinAPI多线程
  4. C++中的Trivial 、POD、non-POD和Standard Layout概念
  5. Ubuntu 16.04 安装jdk
  6. whereis libjpeg.so.7
  7. HackTwo
  8. webform Response的一些成员
  9. [.net 多线程] Interlocked实现CAS操作
  10. 21天学通C++学习笔记(一):入门