如何在Scala的地图中找到(键,值)对的数量? [英] How to find the number of (key , value) pairs in a map in scala?

查看:90
本文介绍了如何在Scala的地图中找到(键,值)对的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的Scala代码中的Map中找到(键,值)对的数量.我可以遍历地图并得到答案,但我想知道是否为此目的有任何直接功能.

I need to find the number of (key , value) pairs in a Map in my Scala code. I can iterate through the map and get an answer but I wanted to know if there is any direct function for this purpose or not.

推荐答案

,您可以使用

you can use .size

scala> val m=Map("a"->1,"b"->2,"c"->3)
m: scala.collection.immutable.Map[String,Int] = Map(a -> 1, b -> 2, c -> 3)

scala> m.size
res3: Int = 3

这篇关于如何在Scala的地图中找到(键,值)对的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆