Home > Back-end >  List is not updated after mapping over it
List is not updated after mapping over it

Time:01-14

I want the output of my first code to add 42 in each number from 1 to 100 and in second code I want it to change any lowercase in name to upper case but the first only prints number from 1 to 100 and second only first letter upper case as it was.

import scala.collection.parallel.CollectionConverters._
object parallelpro1 extends App{
  val list = (1 to 100).toList
  list.par.map(_   42)
  println(list)
  // map
  val lastNames = List("Smith","Jones","Frankenstein","Bach","Jackson","Rodin").par
  lastNames.map(_.toUpperCase())
  println(lastNames)
}

CodePudding user response:

you need to understand that scala collections like List are immutable, list.par.map(_ 42) does not change the list but creates a new one, you need to assign it to a new val, the same applies to lastNames.map(_.toUpperCase()).

  • Related