I'm counting the number of urls within a list. To accomplish this I'm adding to a map where the key is the url and the value is the current counter. Each time I encounter the same key, I increment the counter. Here is the code :
var m = new HashMap[String, Int]
for(l <- MyList){
val url = l.getUrl()
var currentCount : Option[Int] = m.get(url)
currentCount match {
case Some(value) =>
var currentCount = value + 1
m = m ++ Map(url -> currentCount)
case None =>
m = m ++ Map(url -> 1)
}
}
I started with an immutable map but found I needed to reassign the map each time in order to maintain the counter values with the associated keys. Is there a solution to use an immutable map accomplish same task as above ?