大数据学习——scala集合练习

package com

/**
  * Created by ZX on 2016/4/5.
  */
object ListTest {
  def main(args: Array[String]) {
    //创建一个List
    val lst0 = List(1,7,9,8,0,3,5,4,6,2)
    //将lst0中每个元素乘以10后生成一个新的集合
    val lst01=lst0.map(_ * 10)
    println("lst01"+lst01)

    val lst1 = lst0.map(x => x * 2)
    //将lst0中的偶数取出来生成一个新的集合
    val lst02=lst0.filter(_ % 2==0)
    println("lst02"+lst02)

    val lst2 = lst0.filter(x => x % 2 == 0)
    //将lst0排序后生成一个新的集合
    //正序
    val lst03=lst0.sorted
    println("lst03"+lst03)
    //倒序
    val lst04=lst0.sorted.reverse
    println("lst04"+lst04)


    val lst3 = lst0.sorted
    val lst4 = lst0.sortBy(x => x)
    val lst5 = lst0.sortWith((x, y) => x < y)
    //反转顺序
    val lst6 = lst3.reverse
    //将lst0中的元素4个一组,类型为Iterator[List[Int]]

    val lst05=lst0.grouped(4)
//    println("lst05"+lst05.toList)

    val it = lst0.grouped(4)
    //将Iterator转换成List
    val lst7 = it.toList
    //将多个list压扁成一个List
    val lst8 = lst7.flatten

    val lst06=lst05.toList.flatten
    println("lst06"+lst06)

    //先按空格切分,在压平
    val a = Array("a b c", "d e f", "h i j")
    val lst07=a.flatMap(_.split(" "))
    println("lst07"+lst07.toList.flatten)

    a.flatMap(_.split(" "))

    lst0.reduce(_+_)
    lst0.fold(10)(_+_)

    //并行计算求和
    lst0.par.sum
    lst0.par.map(_ % 2 == 0)
    lst0.par.reduce((x, y) => x + y)
    //化简:reduce
    //将非特定顺序的二元操作应用到所有元素
    val lst9 = lst0.par.reduce((x, y) => x + y)
    //按照特定的顺序
    val lst10 = lst0.reduceLeft(_+_)

    //折叠:有初始值(无特定顺序)
    val lst11 = lst0.par.fold(100)((x, y) => x + y)
    //折叠:有初始值(有特定顺序)
    val lst12 = lst0.foldLeft(100)((x, y) => x + y)


    //聚合
    val arr = List(List(1, 2, 3), List(3, 4, 5), List(2), List(0))
    val result = arr.aggregate(0)(_+_.sum, _+_)

    val l1 = List(5,6,4,7)
    val l2 = List(1,2,3,4)
    //求并集
    val r1 = l1.union(l2)
    //求交集
    val r2 = l1.intersect(l2)
    //求差集
    val r3 = l1.diff(l2)
    val r4 = l2.diff(l1)
    println(r1)
    println(r2)
    println(r3)
    println(r4)

    val m = Map(("a", 1))

  }
}

 

    val array = Array(1, 2, 3, 4, 5)
    println(array.fold(0)(_ + _))
//结果15  0+1+2+3+4+5

 

    //并行集合
    println(array.par.sum)
    println(array.par.reduce(_+_))
    println(array.reduceLeft(_+_))
    println(array.foldLeft(10)(_+_))

 

    //聚合
    val arrList=List(List(100,2,3),List(3,4,5),List(4,5))
    val res=arrList.aggregate(10)(_+_.sum,_+_)
    println(res)

 

转载于:https://www.cnblogs.com/feifeicui/p/10967085.html

你可能感兴趣的:(大数据学习——scala集合练习)