简体   繁体   English

如何从给定的一组值生成 map 的随机值,然后将键和值存储到 Scala 中的单独变量中

[英]How to generate the random values of map from a given set of values and then store the key and values into separate variables in scala

I am trying to generate the 1000 random key-val map pairs from the given(statically defined) 2 map key-val pairs in scala and also later i would want to break the key and value pairs and store them into separate variables我正在尝试从 Scala 中给定的(静态定义的)2 个映射键值对生成 1000 个随机键值映射对,后来我想打破键值对并将它们存储到单独的变量中

Whatever i have tried:无论我尝试过什么:

object methodTest extends App{

  val testMap = Map("3875835"->"ABCDE","316067107"->"EFGHI")
  def getRandomElement(seq: Map[String,String]): Map[String,String] = {

    seq(Random.nextInt(seq.length))
  } 
 var outList = List.empty[Map[String,String]]
 for(i<-0 to 1000){
   outList+=getRandomElement(testMap)
 }
 print(outList)
}

The Output should generate 1000 Key-val pairs of map like i am showing below输出应生成 1000 个键值对映射,如下所示

[3875835,ABCDE]
[316067107,EFGHI]
[3875835,ABCDE]
[316067107,EFGHI]
[316067107,EFGHI]
............
............
............. upto 1000 random key-val pairs

Please help me figure out where i am going wrong and let me know How to resolve it, if any issue regarding the requirement, please feel free to comment for it请帮我找出我出错的地方,并让我知道如何解决它,如果有关于要求的任何问题,请随时发表评论

You can transform your seed map testMap into sequence of key/value tuples using .toSeq and then generate key/value pairs by iterating over the list of numbers from 0 until 1000, associating each number to a random choice between first or second element of the seed:您可以使用.toSeq将您的种子映射testMap转换为键/值元组序列,然后通过迭代从 0 到 1000 的数字列表来生成键/值对,将每个数字与第一个或第二个元素之间的随机选择相关联种子:

import scala.util.Random

val testMap = Map("3875835" -> "ABCDE", "316067107" -> "EFGHI")

val seed = testMap.toSeq
val keyValuesList = (0 until 1000).map(index => seed(Random.nextInt(seed.size)))

Note: 0 until 1000 will return all the numbers from 0 to 1000 excluded, so 1000 numbers.注意: 0 until 1000将返回从 0 到 1000 的所有数字,但不包括 1000 个数字。 If you use 0 to 1000 you will get all the numbers from 0 to 1000 included, so 1001 numbers.如果您使用0 to 1000您将获得0 to 1000所有数字,因此是 1001 个数字。

If you want to print the resulting list, you can use .foreach method with println function as argument:如果要打印结果列表,可以使用.foreach方法和println函数作为参数:

keyValuesList.foreach(println)

And you will get:你会得到:

(3875835,ABCDE)
(316067107,EFGHI)
(3875835,ABCDE)
(3875835,ABCDE)
(316067107,EFGHI)
(3875835,ABCDE)
...

if you want to keep only the keys, you can iterate on the list using .map method, taking only the first element of the tuple by using ._1 method, that retrieve the first element of a tuple:如果你只想保留键,你可以使用.map方法迭代列表,使用._1方法只取元组的第一个元素,检索元组的第一个元素:

val keys = keyValuesList.map(keyValuePair => keyValuePair._1)

And if you want only a list containing all second elements of each pair:如果你只想要一个包含每对的所有第二个元素的列表:

val values = keyValuesList.map(keyValuePair => keyValuePair._2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM