• List的map、flatMap、foreach、filter操作代码实战之Scala学习笔记-27


    package com.leegh.dataset

    /**
    * @author Guohui Li
    */
    object List_HighOrder_Function_Ops {
    def main(args: Array[String]): Unit = {
    println(List(1, 2, 3, 4, 6) map (_ + 1))
    val data = List("Scala", "Hadoop", "Spark")
    println(data map (_.length()))
    println(data map (_.toList.reverse.mkString))

    println(data.map(_.toList))
    println(data.flatMap(_.toList))
    println(List.range(1, 10) flatMap (i => List.range(1, i) map (j => (i, j))))

    var sum = 0;
    List(1, 2, 3, 4, 5) foreach (sum += _)
    println("sum:" + sum)

    println(List(1, 2, 3, 4, 6, 7, 8, 9, 10) filter (_ % 2 == 0))
    println(data filter (_.length() == 5))

    println(List(1, 2, 3, 4, 5) partition (_ % 2 == 0))
    println(List(1, 2, 3, 4, 5) find (_ % 2 == 0))
    println(List(1, 2, 3, 4, 5) find (_ < 0))
    println(List(1, 2, 3, 4, 5) takeWhile (_ < 4))
    println(List(1, 2, 3, 4, 5) dropWhile (_ < 4))
    println(List(1, 2, 3, 4, 5) span (_ < 4))

    def hastotallyZeroRow(m: List[List[Int]]) = m exists (row => row forall (_ == 0))
    val m = List(List(1, 0, 0), List(0, 1, 0), List(0, 0, 0))
    println(hastotallyZeroRow(m))
    }
    }

    附:

    本博客说明:

    1.整理思路,提高自己。

    2.受教于王家林老师,​有所收获,故推荐。

    3.博客注重实践,多余的文字就不多说了,都是做技术的。

    4.信息来源于 DT大数据梦工厂微信公众账号:DT_Spark。​

    DT大数据梦工厂的微信公众号是DT_Spark,每天都会有大数据实战视频发布,请您持续学习。

    王家林DT大数据梦工厂scala的所有视频、PPT和代码在百度云盘的链接:http://pan.baidu.com/share/home?uk=4013289088#category/type=0&qq-pf-to=pcqq.group

    王家林《Scala深入浅出实战初级入门经典视频课程》http://edu.51cto.com/lesson/id-66538.html

    王家林《Scala深入浅出实战中级进阶经典视频课程》http://edu.51cto.com/lesson/id-67139.html

  • 相关阅读:
    微信小程序中 数据缓存
    javascript 数组去重方法
    常用网站 ---总结
    vue面试题
    vue创建项目(命令方式)
    shutil 模块:文件复制.移动.压缩.解压.递归删除文件夹
    面向对象一些零散的知识点
    面向对象之多态
    面向对象与继承
    序列化与反序列化的三个模块:json pickle shelve
  • 原文地址:https://www.cnblogs.com/leegh1992/p/4744034.html
Copyright © 2020-2023  润新知