zoukankan      html  css  js  c++  java
  • scala 数据结构(十三):集合实例(二)集合计算高级函数、简化|规约、折叠、对两个map集合之间的数据进行合并

    实例一:

    package com.atguigu.scala.chapter07
    
    /**
      * Author: Felix
      * Date: 2020/5/4
      * Desc: 集合计算高级函数
      */
    object Scala12_TestFun {
      def main(args: Array[String]): Unit = {
        //val list: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
        //(1)过滤 遍历一个集合并从中获取满足指定条件的元素组成一个新的集合
        //对List集合进行遍历,将偶数取出,放到新的集合中去
        //println(list.filter((elem: Int) => {
        //  elem % 2 == 0
        //}))
        //println(list.filter( _ % 2 == 0))
    
        //(2)转化/映射(map) 将集合中的每一个元素映射到某一个函数
        //println(list.map(_ * 2))
    
        //(3)扁平化   :::
        //val nestedList : List[List[Int]]  = List(List(1,2,3),List(4,5,6),List(7,8,9))
        //val newList: List[Int] = nestedList.flatten
        //newList.foreach(println)
    
        //(4)扁平化+映射 注:flatMap相当于先进行map操作,在进行flatten操作  集合中的每个元素的子元素映射到某个函数并返回新集合
    
        //val strings: List[String] = List("hello atguigu","hello jingjing","banzhang jingjing")
        /*
        //对字符串使用空格进行分割,将分割后的单词放到一个新的集合中  List("hello","atguigu","hello","jingjing","banzhang","jingjing")
    
        //先对集合中的元素进行转换   字符串---->数组
        val splitList: List[Array[String]] = strings.map(_.split(" "))
    
        //将集合中的元素由整体 --->个体
        val flatList: List[String] = splitList.flatten
    
        //输出查看效果
        println(flatList)
        */
    
        //使用flatMap函数完成上述功能
        //val flatMapList: List[String] = strings.flatMap(_.split(" "))
        //println(flatMapList)
    
        //(5)分组(groupBy) 按照指定的规则对集合的元素进行分组
        //val list: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
        //val groupMap: Map[Int, List[Int]] = list.groupBy(_%2)
        //println(groupMap)
    
        val nameList: List[String] = List("jingjing","banzhang","banhua","xingda","jiafeng")
    
        val groupMap: Map[Char, List[String]] = nameList.groupBy(_.charAt(0))
        println(groupMap)
    
    
    
      }
    }

    实例二:

    package com.atguigu.scala.chapter07
    
    /**
      * Author: Felix
      * Date: 2020/5/5
      * Desc:
      *   -简化|规约
      *     >通过指定的逻辑将集合中的数据进行聚合,从而减少数据,最终获取结果。
      *     >reduce
      *         底层调用的是reduceLedft
      *         函数参数类型必须一致
      *     >reduceLeft    函数参数类型可以不一致
      *     >reduceRight   函数参数类型可以不一致
      *   -折叠
      *       fold|foldLeft|foldRight
      */
    object Scala13_TestFun {
      def main(args: Array[String]): Unit = {
        //val list: List[Int] = List(1,2,3,4)
        //val res: Int = list.reduce((a:Int,b:Int)=>{a+b})
        //val res: Int = list.reduce(_+_)
        //val res: Int = list.reduceLeft(_+_)
        //val res: Int = list.reduceRight(_+_)
        //println(res)
    
        //println(list.reduceLeft(_ - _))//-8
        //println(list.reduceRight(_ - _))//-2
    
        val list2: List[Int] = List(3,4,5,8,10)
        //val res: Int = list2.reduceRight(_-_)   //6
    
        //println(res)
    
        //折叠   集合外元素和集合内部元素进行聚合
        //val res: Int = list.fold(10)(_+_)
        //println(res)
    
        //println(list.foldLeft(10)(_ + _))
    
        println(list2.foldRight(11)(_ - _))
    
    
    
      }
    }

    实例三:

    package com.atguigu.scala.chapter07
    
    import scala.collection.mutable
    
    /**
      * Author: Felix
      * Date: 2020/5/5
      * Desc: 对两个map集合之间的数据进行合并
      */
    object Scala14_TestFun {
      def main(args: Array[String]): Unit = {
    
        val map1 = mutable.Map("a"->1, "b"->2, "c"->3)
    
        val map2 = mutable.Map("a"->4, "b"->5, "d"->6)
    
        //最终合并后的效果  Map("a"->5,"b"->7,"c"->3,"d"->6)
    
        //技术选型:两个集合之间合并不属于集合内部元素聚合,所以fold更合适有些
    
        /*
        val res: mutable.Map[String, Int] = map1.foldLeft(map2) {
          //mm表示map2,kv表示map1中的每一个元素
          (mm, kv) => {
            //指定合并规则
            val k: String = kv._1
            val v: Int = kv._2
            //根据map1中元素的key,到map2中获取value
            mm(k) = mm.getOrElse(k, 0) + v
            mm
          }
        }
        println(res)
    */
    
        val res: mutable.Map[String, Int] = map2.foldLeft(map1) {
          (mm, kv) => {
            val k: String = kv._1
            val v: Int = kv._2
            mm(k) = mm.getOrElse(k, 0) + v
            mm
          }
        }
        println(res)
      }
    }
  • 相关阅读:
    SSM框架整合(Spring+SrpingMVC+Mybatis) 简单案例
    SpringDataRedis操作Redis简单案例
    SpringMVC总结四:拦截器简单介绍
    Configure a bridge interface over a VLAN tagged bonded interface
    Create a bridge using a tagged vlan (8021.q) interface
    Configure a VLAN (on top of a bond) with NetworkManager (nmcli) in RHEL7
    Configure bridge on a team interface using NetworkManager in RHEL 7
    Configure a bridged network interface for KVM using RHEL 5.4 or later?
    程序员的成长阶梯和级别定义
    <程序员从入门到精通> -- How
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/13809433.html
Copyright © 2011-2022 走看看