如何在科特林的地图上迭代
因此,我是Kotlin的新手,我想知道迭代地图的标准方式是什么。我已经尝试了不同的方法,并且它们似乎都起作用,但是我不知道是否有比其余的更好,或者有一些我不知道的差异。
var mutMap = mutableMapOf("one" to 1, "two" to 2, "tree" to 3, "four" to 4, "five" to 5)
mutMap.forEach { entry -> println(entry) }
mutMap.iterator().forEach { entry -> println(entry) }
mutMap.entries.forEach { entry -> println(entry) }
mutMap.entries.iterator().forEach { entry -> println(entry) }
for (entry in mutMap) { println(entry) }
for (entry in mutMap.entries) { println(entry) }
for (entry in mutMap.iterator()) { println(entry) }
for (entry in mutMap.entries.iterator()) { println(entry) }
另外,如果我还想在迭代时删除一个条目,那么它们都无法正常工作,对吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用迭代器的remove()函数来删除地图项目。
它将打印:
You can use the remove() function of the iterator to remove the item of map.
It'll prints:
如果您浏览kotlin的 collections 包装包装有很多whoooole poxpare您可以使用的东西,是的!许多不同的功能使您可以深入研究特定的数据(例如键或值与条目,或提供索引)或在处理集合时获得特定的行为。
您给出的示例基本上都是同一回事。 函数在各种类型的集合上:
这是这些集合的源代码(每个函数下都有一个源链接 很有用!
主页, rel =“ nofollow noreferrer”> basic for 循环,正如文档所说迭代。您的示例基本上与正在发生的事情相同,只是在 foreach -&gt中的各个点跳入;循环基础 - >获取迭代器进程。
唯一不同的部分是当您调用
条目
时,返回set
保留键/值entry
配对 - 因此您正在迭代那,而不是地图
本身。但是,等等,如果您在MAP
上调用iterator()
,会发生什么?它使用
条目
本身!所以,是的,他们都是一样的,我认为这取决于这
hasnext()
在其上或任何结果
actibal时(这与fold <
,但无论如何更可比) /代码>
foreach条目
map 意味着您在链条链时要明确有关正在使用的内容foreach
上您如何指定键
或值仅与这些范围 for each 相同,但它更清楚If you browse through Kotlin's Collections package there is a whoooole lot of stuff you can use, yeah! Lots of different functions that let you drill down into specific pieces of data (like keys or values vs entries, or providing indices) or getting specific behaviour as you process a collection.
The examples you've given are all basically the same thing though. Here's the page for all the
forEach
functions on the various types of collections:And here's the source code for those (there's a source link under every function's main page, useful to know about! You can see exactly how they work)
So really they're all wrappers for a basic
for
loop, which as the documentation says, iterates through anything that provides an iterator. Your examples are all basically the same thing that's happening, just jumping in at various points in the forEach -> basic for loop -> get an iterator process.The only part that's different is when you call
entries
, which returns aSet
holding the key/valueEntry
pairs - so you're iterating over that, rather than theMap
itself. But wait, what does happen if you calliterator()
on aMap
?It uses
entries
itself! So yeah they're all the same thingReally I think it comes down to this
iterator()
on anything unless you know you need one for some reason, like you're going to be callinghasNext()
on it or whateverforEach
fits with Kotlin's more declarative style, can be chained, and automatically passes in variables instead of you having to declare themfor
loop is more readable for what you're doing though, especially if you're modifying some kind ofresult
variable (which is more comparable to afold
than aforEach
but anywayentries
on aMap
means you're being explicit about what you're working with when you chain aforEach
onto it, similar to how you can specifykeys
orvalues
and only work with those. It's the same as just callingforEach
on the map itself, but it's clearer