Scala——5. Scala List练习

Scala List

  • map、filter、sorted、grouped、flatten、flatMap、reduce、fold
// 创建一个List
scala> val l1=List(1,2,3,4,5)
l1: List[Int] = List(1, 2, 3, 4, 5)

// 将l1中的每一个元素乘以10后生成一个新的集合l2
scala> val l2 = for (i <- l1) yield i * 10
l2: List[Int] = List(10, 20, 30, 40, 50)

scala> val f1 = (x:Int) => x*10
f1: Int => Int = <function1>

scala> l1.map(f1)
res38: List[Int] = List(10, 20, 30, 40, 50)

scala> l1.map(x => x*10)
res39: List[Int] = List(10, 20, 30, 40, 50)

scala> l1.map(_*10)
res40: List[Int] = List(10, 20, 30, 40, 50)

// 将l1中的偶数取出来生成一个新的集合
scala> l1.filter((x:Int) => x%2 ==0)
res41: List[Int] = List(2, 4)

scala> l1.filter(_ % 2 == 0)
res42: List[Int] = List(2, 4)

scala> l1.filter(_ % 2 == 1)
res43: List[Int] = List(1, 3, 5)

// 将l1排序后生成一个新的集合
scala> val l1=List(2,1,3,5)
l1: List[Int] = List(2, 1, 3, 5)

scala> l1.sorted
res44: List[Int] = List(1, 2, 3, 5)

scala> l1.sorted.reverse
res45: List[Int] = List(5, 3, 2, 1)

scala> l1.sortBy(x => x)
res46: List[Int] = List(1, 2, 3, 5)

scala> l1.sortWith((x,y) => x<y)
res47: List[Int] = List(1, 2, 3, 5)

scala> l1.sortWith((x,y) => x>y)
res48: List[Int] = List(5, 3, 2, 1)

scala> l1.sortWith(_ > _)
res49: List[Int] = List(5, 3, 2, 1)

scala> l1.sortWith(_ < _)
res50: List[Int] = List(1, 2, 3, 5)

scala> val ll = l1.reverse
ll: List[Int] = List(5, 3, 1, 2)

scala> var list0=List(1,7,9,8,0,3,5,4,6,2)
list0: List[Int] = List(1, 7, 9, 8, 0, 3, 5, 4, 6, 2)

// 将list0中的元素4个一组,类型为Iterator[List[Int]]
scala> val list7=list0.grouped(4)
list7: Iterator[List[Int]] = non-empty iterator

// 将Iterator转换成List
scala> val list7=list0.grouped(4).toList
list7: List[List[Int]] = List(List(1, 7, 9, 8), List(0, 3, 5, 4), List(6, 2))

scala> list7
res51: List[List[Int]] = List(List(1, 7, 9, 8), List(0, 3, 5, 4), List(6, 2))

// 将多个list压扁成一个List
scala> list7.flatten
res52: List[Int] = List(1, 7, 9, 8, 0, 3, 5, 4, 6, 2)

scala> val lines = List("hello tom hello jerry", "hello jerry", "hello kitty")
lines: List[String] = List(hello tom hello jerry, hello jerry, hello kitty)

scala> lines.map(x => x.split(" "))
res53: List[Array[String]] = List(Array(hello, tom, hello, jerry), Array(hello, jerry), Array(hello, kitty))

scala> lines.map(x => x.split(" ")).flatten
res54: List[String] = List(hello, tom, hello, jerry, hello, jerry, hello, kitty)

// 先按空格切分,再压平,本质上是由两个方法组合而成的
// 先执行map,对每一个元素进行按照指定的函数要求处理
// 再执行flatten,对map处理完的结果进行压平操作
scala> lines.flatMap(x => x.split(" "))
res55: List[String] = List(hello, tom, hello, jerry, hello, jerry, hello, kitty)

scala> lines.flatMap(_.split(" "))
res56: List[String] = List(hello, tom, hello, jerry, hello, jerry, hello, kitty)

scala> val l1 = List(1,2,3,4)
l1: List[Int] = List(1, 2, 3, 4)

scala> l1.reduce((x,y) => x+y)
res57: Int = 10

scala> (((1+2) +3) +4)
res58: Int = 10

scala> l1.reduce(_+_)
res59: Int = 10

scala> l1.reduce(_-_)
res60: Int = -8

scala> l1.reduceLeft(_+_)
res61: Int = 10

scala> l1.reduceLeft(_-_)
res62: Int = -8

scala> l1.reduceRight(_+_)
res63: Int = 10

scala> val l1 = List(2,1,3,1)
l1: List[Int] = List(2, 1, 3, 1)

scala> l1.reduceRight(_-_)
res64: Int = 3

scala> 2-(1-(3-1))
res65: Int = 3

scala> val list0=List(1,2,3,5)
list0: List[Int] = List(1, 2, 3, 5)

scala> list0.reduce(_+_)
res67: Int = 11

scala> list0.fold(0)(_+_)
res68: Int = 11

scala> list0.fold(4)(_+_)
res69: Int = 15

scala> list0.foldLeft(4)(_-_)
res70: Int = -7

scala> list0.foldRight(4)(_-_)
res72: Int = 1

scala> 1-(2-(3-(5-4)))
res73: Int = 1

scala> (4 /: list0)(_-_)
res74: Int = -7

scala> (list0 :\ 4)(_-_)
res75: Int = 1

  • 两集合并集、差集、交集
scala> val l1 = List(1,2,3,4)
l1: List[Int] = List(1, 2, 3, 4)

scala> val l2 = List(3,4,5,6)
l2: List[Int] = List(3, 4, 5, 6)

并集
scala> l1.union(l2)
res288: List[Int] = List(1, 2, 3, 4, 3, 4, 5, 6)

差集
scala> l1.diff(l2)
res289: List[Int] = List(1, 2)

交集
scala> l1.intersect(l2)
res290: List[Int] = List(3, 4)

  • 并行计算

arr.fold(0)(+)与reduce的区别是有初始值,当初始值为0,效果一样
arr.par.fold
几个线程就会加进去几个初始值

scala> val list = List(1,2,3,4,5)
list: List[Int] = List(1, 2, 3, 4, 5)

scala> list.fold(10)(_+_)
res0: Int = 25

scala> list.par.fold(10)(_+_)
res1: Int = 65

scala> list.par.fold(10)(_+_)
res2: Int = 55

以下代码获取到参与并行计算的线程:
scala> (0 to 10000).collect{case _ => Thread.currentThread.getName}.distinct
res3: scala.collection.immutable.IndexedSeq[String] = Vector(main)

scala> (0 to 10000).par.collect{case _ => Thread.currentThread.getName}.distinct
res4: scala.collection.parallel.immutable.ParSeq[String] = ParVector(ForkJoinPool-1-worker-5, ForkJoinPool-1-worker-13, ForkJoinPool-1-worker-15, ForkJoinPool-1-worker-11, ForkJoinPool-1-worker-1, ForkJoinPool-1-worker-9, ForkJoinPool-1-worker-3, ForkJoinPool-1-worker-7)

你可能感兴趣的:(Scala)