美文网首页
Scala基础——容器操作

Scala基础——容器操作

作者: _羊羽_ | 来源:发表于2019-08-12 22:09 被阅读0次

    遍历foreach

    list 遍历

    var l = List(1,2,3,4,5)
    l.foreach(i=>println(i))
    

    map 遍历

        var m = Map("tony" -> 80, "bob" -> 90)
        m.foreach(kv => println(kv._1, kv._2))
    

    映射map

        val l = List("hive","hadoop","spark")
        val books= l.map(s=>s.toUpperCase)
        for(x <- books){
          println(x)
        }
    

    flatMap

        val l = List("hive","hadoop","spark")
        val books= l.flatMap(s=>s.toList)
        for(x <- books){
          println(x)
        }
    

    过滤filter

        val l = List(1,2,3,4,5)
        val result= l.filter(_%2==0)
        println(result)
    

    find

        val l = List("hive","hadoop","spark")
        val result= l.find(_ startsWith "h")
        println(result)
    

    exists

    val l = List("hive","hadoop","spark")
    val result= l.exists(_ startsWith "h")
    

    reduce

        val l = List(1,2,3,4,5)
        val result= l.reduce(_+_)
        println(result)
    

    reduceLeft

     val l = List(1,2,3,4,5)
    val left= l.reduceLeft(_-_)
    

    reduceRight

        val l = List(1,2,3,4,5)
        val right= l.reduceRight(_-_)
        println(right)
    

    相关文章

      网友评论

          本文标题:Scala基础——容器操作

          本文链接:https://www.haomeiwen.com/subject/pblsjctx.html