Scala 学习笔记之集合(1)

mac2022-06-30  62

1 package com.citi.scala 2 3 object CollectionDemo { 4 def main(args: Array[String]): Unit = { 5 6 /** 7 * List 8 */ 9 println("--------------------------List-------------------------") 10 val numbers = List[Int](11, 22, 33, 44, 55) 11 val numbers1 = List(11, 22, 33, 44, 55) 12 val colors = List[String]("red", "green", "blue") 13 println(s"colors has ${colors.size}, $colors") 14 //取第一个 15 println(colors.head) 16 //去掉第一个,取剩下的 17 println(colors.tail) 18 //取第二个 19 println(colors(1)) 20 //遍历 21 for (s <- colors) { println(s) } 22 //高阶函数使用 23 colors.foreach { (s: String) => println(s) } 24 colors.foreach { s => println(s) } 25 colors.foreach { println(_) } 26 val elSize = colors.map((s: String) => s.size) 27 println(elSize) 28 println(numbers.reduce((a: Int, b: Int) => a + b)) 29 //初始化空List 30 val ls: List[Int] = List[Int]() 31 val lls: List[Int] = Nil 32 println(ls == lls) 33 //遍历 34 var i = numbers 35 while (!i.isEmpty) { 36 println(i.head) 37 i = i.tail 38 } 39 40 i = numbers 41 while (i != Nil) { 42 println(i.head) 43 i = i.tail 44 } 45 //cons操作符 46 val consNumbers = 3 :: 2 :: 1 :: Nil 47 println(consNumbers) 48 49 //List的算数运算 50 println(3 :: 2 :: 1 :: Nil) 51 println(List(7, 6, 5) ::: List(4, 3, 2, 1)) 52 println(List(4, 3, 2, 1) ++ List(7, 6, 5)) 53 println(List(1, 2) == List(2, 1)) 54 println(List(2, 1) == List(2, 1)) 55 println(List(1, 2, 3, 2, 1).distinct) 56 println(List(1, 2, 3, 2, 1) drop (2)) 57 println(List(4, 3, 2, 1) filter { _ >= 3 }) 58 println(List(List(1, 2, 3), List(4, 5, 6)).flatten) 59 60 println((List(1, 2, 3, 4, 5).partition(_ >= 3))) 61 println(List(1, 2, 3, 4, 5) reverse) 62 println(List(2, 3, 5, 7, 10, 11) slice (1, 4)) 63 println(List("apple", "ben") sortBy { s => s.size }) 64 println(List(2, 3, 5, 7, 10, 11) splitAt (3)) 65 println(List(2, 3, 5, 7, 10, 11) take (3)) 66 println(List(1, 2, 3) zip List("a", "b")) 67 println(List(1, 2) :+ 3) 68 69 /** 70 * Set 71 */ 72 println("--------------------------Set-------------------------") 73 val numbersets = Set[Int](11, 22, 11, 33, 44, 55) 74 println(s"numbersets has ${numbersets.size}, $numbersets") 75 println(numbersets.reduce((a: Int, b: Int) => a + b)) 76 /** 77 * Map 78 */ 79 println("--------------------------Map-------------------------") 80 val colorMap = Map[String, Int]("red" -> 1, "green" -> 2, "yellow" -> 3) 81 println(colorMap("red")) 82 for (pair <- colorMap) { println(pair) } 83 84 } 85 }

 

运行结果:

--------------------------List-------------------------colors has 3, List(red, green, blue)redList(green, blue)greenredgreenblueredgreenblueredgreenblueredgreenblueList(3, 5, 4)165true11223344551122334455List(3, 2, 1)List(3, 2, 1)List(7, 6, 5, 4, 3, 2, 1)List(4, 3, 2, 1, 7, 6, 5)falsetrueList(1, 2, 3)List(3, 2, 1)List(4, 3)List(1, 2, 3, 4, 5, 6)(List(3, 4, 5),List(1, 2))List(5, 4, 3, 2, 1)List(3, 5, 7)List(ben, apple)(List(2, 3, 5),List(7, 10, 11))List(2, 3, 5)List((1,a), (2,b))List(1, 2, 3)--------------------------Set-------------------------numbersets has 5, Set(33, 22, 44, 11, 55)165--------------------------Map-------------------------1(red,1)(green,2)(yellow,3)

转载于:https://www.cnblogs.com/AK47Sonic/p/7073855.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)