单个集合通用方法6(groupBy)

groupBy

    val array: Array[Int] = Array(1,2,3,4,5)
    val list = List(1,2,3,4)
    val map = Map(("a",1),("b",2),("c",3))
    val set = Set(1,3,4,5)
    val tuple: (Int, Int, Int, Int) = (1,2,3,4)

    //Map(分组key -> List(元素))
    println(list.groupBy(num => num))
    println(list.groupBy(num => num%2))

    val strings = List("Hello", "Hive", "Hbase","Spark","Scala")
    println(strings.groupBy(_.substring(0, 1)))


Map(2 -> List(2), 4 -> List(4), 1 -> List(1), 3 -> List(3))
Map(1 -> List(1, 3), 0 -> List(2, 4))
Map(S -> List(Spark, Scala), H -> List(Hello, Hive, Hbase))

你可能感兴趣的:(单个集合通用方法6(groupBy))