简体   繁体   English

Scala迭代元组图

[英]Scala Iterating Tuple Map

I have a map of key value pairs and I am trying to fetch the value given a key, however although it returns me a value it comes along with some, any idea how can I get rid of it? 我有一个键值对的映射,我试图获取给定键的值,但是尽管它返回了一个它附带的值,但是我知道如何摆脱它吗? My code is: 我的代码是:

val jsonmap = simple.split(",\"").toList.map(x => x.split("\":")).map(x => Tuple2(x(0), x(1))).toMap
val text = jsonmap.get("text")

Where "text" is the key and I want the value that it is mapped to, I am currently getting the following output: 当“文本”是键,并且我想将其映射到值时,我目前得到以下输出:

Some("4everevercom")    

I tried using flatMap instead of Map but it doesn't work either 我尝试使用flatMap而不是Map,但是它也不起作用

You are looking for the apply method. 您正在寻找apply方法。 Seq , Set and Map implement different apply methods, the difference being the main distinction between each one. SeqSetMap实现不同的apply方法,不同之处是每种方法之间的主要区别。

As a syntactic sugar, .apply can be omitted in o.apply(x) , so you'd just have to do this: 作为语法糖, .applyo.apply(x)可以省略,因此您只需要这样做:

val text = jsonmap("text") // pass "key" to apply and receive "value"

For Seq , apply takes an index and return a value, as you used in your own example: 对于Seqapply接受一个索引并返回一个值,如您在自己的示例中所使用的:

x(0), x(1)

For Set , apply takes a value, and returns a Boolean . 对于Setapply接受一个值,然后返回Boolean

You can use getOrElse, as @Brian stated , or 'apply' method and deal with absent values on your own: 您可以使用@Brian 所说的 getOrElse,也可以使用'apply'方法并自行处理缺少的值:

val text: String = jsonmap("text")
// would trow java.util.NoSuchElementException, if no key presented

Moreover, you can set some default value so Map can fallback to it, if no key found: 此外,您可以设置一些默认值,以便在找不到键的情况下,Map可以回退到该默认值:

val funkyMap = jsonmap.get("text").withDefaultValue("")
funkyMap("foo")
// if foo or any other key is not presented in map, "" will be returned

Use it, if you want to have default value for all missing keys. 如果要为所有丢失的键设置默认值,请使用它。

You can use getOrElse which gets the value of Some if there is one or the default value that you specify. 如果有一个或指定的默认值,则可以使用getOrElse来获取Some的值。 If the key exists in the map, you will get the value from Some . 如果键存在于映射中,则将从Some获取值。 If the key does not exist, you will get the value specified. 如果键不存在,您将获得指定的值。

scala> val map = Map(0 -> "foo")
map: scala.collection.immutable.Map[Int,String] = Map(0 -> foo)

scala> map.get(0)
res3: Option[String] = Some(foo)

scala> map.getOrElse(1, "bar")
res4: String = bar

Also, see this for info on Scala's Some and None Options: http://www.codecommit.com/blog/scala/the-option-pattern 另外,有关Scala的“某些”和“无”选项的信息,请参见此: http : //www.codecommit.com/blog/scala/the-option-pattern

Just offering additional options that might help you work with Option and other similar structures you'll find in the standard library. 仅提供其他选项可能会帮助您使用标准库中的Option和其他类似结构。

You can also iterate (map) over the Option. 您还可以在Option上进行迭代(映射)。 This works since Option is an Iterable containing either zero or one element. 这是Iterable ,因为Option是一个包含零个或一个元素的Iterable

myMap.get(key) map { value =>
  // use value...
}

Equivalently, you can use a for if you feel it makes for clearer code: 同样,如果您认为for可以使用更清晰的代码for则可以使用for

for (value <- myMap.get(key)) {
  // use value...
}

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

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