Content preview from Head First Kotlin程序设计

394 第
12
章
工具箱
Kotlin
工具箱
现在你已经学完了第
12
章
,
工具箱中
又多了些内置高阶函数
。
第
12
章
使用
minBy
和
maxBy
找出集合中的最小值
或最大值
。
这些函数接受一个参数
:
一个
在其方法体中指定函数条件的
lambda
。
它
们的返回类型与集合元素的类型一致
。
使用
sumBy
或
sumByDouble
返回集合元素
的总和
。
它的参数是一个描述求和对象的
lambda
。
如
果是
Int
,
则
使用
sumBy
,
如果
是
Double
,
则使用
sumByDouble
。
filter
函数允许你根据一些条件查询或
过滤集合
。
lambda
用于指定条件
,
并且其
方法体必须返回
Boolean
值
。
filter
通
常返回一个
List
。
不过
,
如果该函数用于
Map
,
那么它将返回一个
Map
。
map
函数根据一些由
lambda
指定的条件转
换集合元素
。
它将返回一个
List
。
forEach
的功能与
for
循环类似
。
它允许你
对集合的每个元素执行一个或多个操作
。
使用
groupBy
将集合划分成几组
。
它接受
一个
lambda
作为参数
,
lambda
定义了分
组规则
。
该函数返回一个
Map
,
lambda
条
件是
Map
的
key
,
并且每个
value
都是一个
List
。
fold
函数允许你指定初始值
,
并对集合
的每个元素进行一些操作
。
它接受两个参
数
:
初始值和用于指定操作的
lambda
。
本章重点
虽然你的离开令人难过
,
但是什么也比不上学以致用
。
本书的最后还有三
个附录
,
读完全书就到了实践新知识的时候了
。
祝你一路顺风
!
说声再见
很高兴在
K
otlin
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access
More than 5,000 organizations count on O’Reilly
O’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.Julian F.
I wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.Addison B.
I’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.Amir M.
I'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.Mark W.
Publisher Resources
ISBN: 9787111662358