scala 如何在scala的地图中找到(键,值)对的数量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24952447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find the number of (key , value) pairs in a map in scala?
提问by Tanvi
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.
我需要在我的 Scala 代码的 Map 中找到 (key , value) 对的数量。我可以遍历地图并得到答案,但我想知道是否有任何直接用于此目的的函数。
回答by Govind Singh
回答by user2864740
Use Map#size:
使用Map#size:
The size of this traversable or iterator.
此可遍历或迭代器的大小。
The sizemethod is from TraversableOnce so, barring infinite sequences or sequences that shouldn't be iterated again, it can be used over a wide range - List, Map, Set, etc.
该size方法来自 TraversableOnce,因此,除了无限序列或不应再次迭代的序列之外,它可以在广泛的范围内使用 - List、Map、Set 等。

