简体   繁体   English

Scala Map [(String,String),Int]建立对应的MAP [String,String]

[英]scala Map[(String, String),Int] to build corresponding MAP[String,String]

How to transform: 如何转换:

(Map(UserLang -> en, UserName -> a),1)
(Map(UserLang -> jp, UserName -> b),1)

to

 (UserLang -> en, UserName -> a)
 (UserLang -> jp, UserName -> b)

How to do this via functional programming 如何通过功能编程做到这一点

Try something like this: 尝试这样的事情:

val map1: Map[(String, String),Int]
val map2: Map[String, String] = map1.keySet.toMap

.keySet discards the Int and turns your Map[(String, String),Int] into a Set[(String,String)] which you can then easily convert to a Map by calling toMap . .keySet丢弃Int并将Map[(String, String),Int]转换为Set[(String,String)] ,然后可以通过调用toMap轻松将其转换为Map

(Map(UserLang -> en, UserName -> a),1) is type (Map[String,String], Int) and not Map[(String,String),Int] as stated in the question title. (Map(UserLang -> en, UserName -> a),1)的类型为(Map[String,String], Int)而不是Map[(String,String),Int]如问题标题中所述。

For the former, try map(_._1) . 对于前者,请尝试map(_._1) For the latter try keys.toMap . 对于后者,请尝试keys.toMap

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

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