Anand
Anand

Reputation: 645

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

Upvotes: 1

Views: 209

Answers (2)

jwvh
jwvh

Reputation: 51271

(Map(UserLang -> en, UserName -> a),1) is type (Map[String,String], Int) and not Map[(String,String),Int] as stated in the question title.

For the former, try map(_._1). For the latter try keys.toMap.

Upvotes: 1

Luka Jacobowitz
Luka Jacobowitz

Reputation: 23532

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 Mapby calling toMap.

Upvotes: 1

Related Questions