当前位置:网站首页>Scala111-map、flatten、flatMap
Scala111-map、flatten、flatMap
2022-07-25 15:01:00 【维格堂406小队】
Intro
map、flatten、flatMap是scala中常用的函数,举几个例子,简单的总结下。
Map
map操作是针对集合的变换操作,它将函数应用到集合中的每个元素,并产生一个结果集合。
import scala.collection.mutable.{
ListBuffer, ArrayBuffer}
import scala.collection.mutable.{ListBuffer, ArrayBuffer}
val arr=Array(ArrayBuffer(1),ArrayBuffer(2),ArrayBuffer(3),ArrayBuffer(4),ArrayBuffer(5))
arr: Array[scala.collection.mutable.ArrayBuffer[Int]] = Array(ArrayBuffer(1), ArrayBuffer(2), ArrayBuffer(3), ArrayBuffer(4), ArrayBuffer(5))
生成一个ArrayBuffer的嵌套数组,里面的元素也是ArrayBuffer
通过map,给每个元素ArrayBuffer增加10
arr.map(x=>x+=(10)).foreach(println)
ArrayBuffer(1, 10)
ArrayBuffer(2, 10)
ArrayBuffer(3, 10)
ArrayBuffer(4, 10)
ArrayBuffer(5, 10)
flatten
flatten可以把嵌套的结构展开。比如上面的case中,把所有子元素ArrayBuffer展开,拼接成一个大的数组
val arr1 = arr.flatten
arr1: Array[Int] = Array(1, 10, 2, 10, 3, 10, 4, 10, 5, 10)
arr1.getClass.getSimpleName
res37: String = int[]
flatMap
如果想一步完成上面map和flatten的操作,咋整,flatMap可以做到
val arr=Array(ArrayBuffer(1),ArrayBuffer(2),ArrayBuffer(3),ArrayBuffer(4),ArrayBuffer(5))
arr: Array[scala.collection.mutable.ArrayBuffer[Int]] = Array(ArrayBuffer(1), ArrayBuffer(2), ArrayBuffer(3), ArrayBuffer(4), ArrayBuffer(5))
arr.flatMap(_+=(10))
res38: Array[Int] = Array(1, 10, 2, 10, 3, 10, 4, 10, 5, 10)
Ref
[1] http://dblab.xmu.edu.cn/blog/961-2/
2021-03-11 于南京市江宁区九龙湖
边栏推荐
- Implement a simple restful API server
- Gson and fastjson
- Number of high-quality number pairs [bit operation characteristics + abstract ability evaluation + grouping fast statistics]
- 43 box model
- "Ask every day" briefly talk about JMM / talk about your understanding of JMM
- EDA chip design solution based on AMD epyc server
- Pl/sql creates and executes ORALCE stored procedures and returns the result set
- BigDecimal rounds the data
- IP地址分类,判断一个网段是子网超网
- 阿里云技术专家邓青琳:云上跨可用区容灾和异地多活最佳实践
猜你喜欢

Client error: invalid param endpoint is blank

"How to use" agent mode

41 picture background synthesis - colorful navigation map

51 single chip microcomputer learning notes (1)

Splice a field of the list set into a single string

37 element mode (inline element, block element, inline block element)
![[MySQL series] - how much do you know about the index](/img/d7/5045a846580be106e2bf16d7b30581.png)
[MySQL series] - how much do you know about the index

阿里云技术专家邓青琳:云上跨可用区容灾和异地多活最佳实践

Yarn: the file yarn.ps1 cannot be loaded because running scripts is prohibited on this system.

"Ask every day" how locksupport realizes thread waiting and wakeup
随机推荐
SSM Advanced Integration
44 Sina navigation, Xiaomi sidebar exercise
27 选择器的分类
Leetcode-198- house raiding
Thymeleaf notes
Sudo rosdep init error ROS installation problem solution
Melody + realsense d435i configuration and error resolution
万能通用智能JS表单验证
Gonzalez Digital Image Processing Chapter 1 Introduction
006操作符简介
Leetcode combination sum + pruning
Add the jar package under lib directory to the project in idea
LeetCode-198-打家劫舍
IP address classification, which determines whether a network segment is a subnet supernetwork
32 use of chrome debugging tools
Nacos2.1.0 cluster construction
String type time comparison method with error string.compareto
When using jetty to run items, an error is reported: form too large or form too many keys
easygui使用的语法总结
06. Neural network like