D.L.
D.L.

Reputation: 169

How to convert one map to another map in scala

I have ane map that maps ids to integers. I need to create another map which will map ids to a particular type (interval) which has a tuple of integers.

val secondValue = 5
input: Map[Identifier, Integer]
val newInput: Map[Identifier, Interval] = input.map({
       case (x, d) => (x -> Interval(d, secondValue))
})

Interval is defined in a file and it is imported here. This code does not work as newInput is not changed. Can you guide me where I am doing wrong?

The required type of input is Identifier -> Integer and newInput is Identifier -> [Integer, Integer], but I am getting Identifier -> Integer for both input and newInput.

Upvotes: 1

Views: 4997

Answers (1)

F. Lins
F. Lins

Reputation: 634

Since you are not providing Identifier and Interval, I created my own. After initializing input properly, your code seems to work fine:

case class Interval(x: Int, d: Int)
case class Identifier(x:Int)

val secondValue = 5
val input: Map[Identifier, Integer] = Map(Identifier(1) -> 10)

val newInput: Map[Identifier, Interval] = input.map({
  case (x, d) => 
    x -> Interval(d, secondValue)
})

Upvotes: 4

Related Questions