簡體   English   中英

Scala 將 Array[Int] 轉換為 Map[Int, Int] 的功能樣式

[英]Scala Functional style of converting Array[Int] to Map[Int, Int]

我想將 Array[Int] 轉換為 Map[Int, Int] ,其中每個鍵是數組的元素,相應的值是數組中元素的索引。

Array(11, 12, 13) => Map((11,0), (12,1), (13,2))

是否可以在不使用具有更多功能風格的可變 map 的情況下做到這一點?

例如:

myArray.toMap(implicit def (... ))
scala> Array(11, 12, 13).zipWithIndex.toMap
res0: scala.collection.immutable.Map[Int,Int] = Map(11 -> 0, 12 -> 1, 13 -> 2)

PS But use HashMap / HashSet from Scala collections wisely to avoid security vulnerabilities on untrusted input under DoS/DoW attacks that exploit hash collision vulnerabilities of them directly or indirectly through Scala collections methods that use them under hood: toMap , keys , distinct , groupBy , ETC..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM