user1615666
user1615666

Reputation: 3451

scala get first key from seq of map

In scala, I know the mySeq is an array of Map object and the array only has one element. then I want to get first key of this element. Why it doesn't work ? it gave me error: value keySet is not a member of (Int, String)

code:

val mySeq: Seq[(Int, String)] = ... 
        val aMap = mySeq(0)
        val firstKey = aMap.keySet.head

Upvotes: 1

Views: 1946

Answers (1)

Jordão
Jordão

Reputation: 56467

That's actually a Seq of tuples:

val aTuple = mySeq(0)
val firstKey = aTuple._1

To declare a Seq or maps, you'd use:

val mySeq: Seq[Map[Int, String]] = ... 

But note that it doesn't make much sense to get the first key of a map, since maps are usually unordered by design.

Upvotes: 4

Related Questions