前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何使用Scala的exists函数

如何使用Scala的exists函数

作者头像
九州暮云
发布2019-08-21 10:52:42
1.9K0
发布2019-08-21 10:52:42
举报
文章被收录于专栏:九州牧云

在本文中,我们将演示如何在Scala的集合上使用exists函数,该函数适用于Scala的可变(Mutable)和不可变(Immutable)集合。

exists函数接受谓词函数(predicate function),并将使用该函数查找集合中与谓词匹配的第一个元素。

Scala文档中exists函数的定义如下:

代码语言:javascript
复制
def exists(p: (A) ⇒ Boolean): Boolean

exists函数是IterableLike特质(trait)的一个成员。

示例

1、如何初始化甜甜圈序列(a Sequence of donuts)

下面的代码演示了如何初始化一个包含String类型元素的甜甜圈序列:

代码语言:javascript
复制
println("Step 1: How to initialize a Sequence of donuts")
val donuts: Seq[String] = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut")
println(s"Elements of donuts = $donuts")

输出:

代码语言:javascript
复制
Step 1: How to initialize a Sequence of donuts
Elements of donuts = List(Plain Donut, Strawberry Donut, Glazed Donut)

2、使用exists函数如何检查在序列中是否存在一个指定的元素

下面的代码展示了如何使用exists函数来查找某个特定元素是否存在于一个序列中——更准确地说,就是使用exists函数来查找甜甜圈序列中存在普通甜甜圈元素:

代码语言:javascript
复制
println("\nStep 2: How to check if a particular element exists in the sequence using the exists function")
val doesPlainDonutExists: Boolean = donuts.exists(donutName => donutName == "Plain Donut")
println(s"Does Plain Donut exists = $doesPlainDonutExists")

输出:

代码语言:javascript
复制
Step 2: How to check if a particular element exists in the sequence using the exists function
Does Plain Donut exists = true

3、如何为exists函数声明谓词值函数

下面的代码展示了如何声明谓词值函数来查找序列中是否存在普通的甜甜圈元素:

代码语言:javascript
复制
println("\nStep 3: How to declare a predicate value function for the exists function")
val plainDonutPredicate: (String) => Boolean = (donutName) => donutName == "Plain Donut"
println(s"Value function plainDonutPredicate = $plainDonutPredicate")

输出:

代码语言:javascript
复制
Step 3: How to declare a predicate value function for the exists function
Value function plainDonutPredicate = <function1>

注意: 谓词函数是一个值函数(Value Function

4、如何使用exists函数并通过步骤3的谓词函数查找元素Plain Donut

下面的代码展示了如何调用exists方法并传递步骤3中的值谓词函数,以查找甜甜圈序列中是否存在普通的甜甜圈元素:

代码语言:javascript
复制
println("\nStep 4: How to find element Plain Donut using the exists function and passing through the predicate function from Step 3")
println(s"Does Plain Donut exists = ${donuts.exists(plainDonutPredicate)}")

输出:

代码语言:javascript
复制
Step 4: How to find element Plain Donut using the exists function and passing through the predicate function from Step 3
Does Plain Donut exists = true

5、如何为exists函数声明谓词def函数

下面的代码展示了如何使用谓词def函数查找序列中是否存在普通的甜甜圈元素:

代码语言:javascript
复制
println("\nStep 5: How to declare a predicate def function for the exists function")
def plainDonutPredicateFunction(donutName: String): Boolean = donutName == "Plain Donut"

6、如何使用exists函数并通过步骤5中的谓词def函数查找元素Plain Donut

下面的代码展示了如何调用exists方法并通过步骤5中的def谓词函数查找甜甜圈序列中是否存在普通的甜甜圈元素:

代码语言:javascript
复制
println("\nStep 6: How to find element Plain Donut using the exists function and passing through the predicate function from Step 5")
println(s"Does plain Donut exists = ${donuts.exists(plainDonutPredicateFunction(_))}")

输出:

代码语言:javascript
复制
Step 6: How to find element Plain Donut using the exists function and passing through the predicate function from Step 5
Does plain Donut exists = true

编译自:Scala Tutorial - Learn How To Use Exists Function

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 示例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档