scala> userMap.values.exists{ case User(_, surName1, _, _ ) => true }
res1: Boolean = true
But if you're going to make a change, don't check for true then do it. That's imperative non-scala way of thinking. Here's a mildly evil way of doing that:
var changed = false
userMap map { x => x match {
case (n, User(i, f, "surName1", p)) => {
changed = true
(n, User(i, f, "Appleyard", p))
}
case _ => x
}
}
What this does is return a copy of userMap (because your userMap is probably immutable unless you specified a mutable map), with any "surName1" users changed to "Appleyards". If any matching users were found, "changed" will be set to true as a side-effect. I call this mildly evil because it uses side effects. Mind you, there's a very simple way of avoiding the side-effect...
scala> newMap = userMap map { x => x match {
case (n, User(i, f, "surName1", p)) => {
(n, User(i, f, "Appleyard", p))
}
case _ => x
}
}
scala> newMap == userMap
res2: Boolean = false
Now here's something more formal
def surnameA2B(oldName: String, newName: String, m: Map[Int,User]): (Int, Map[Int,User]) = {
val changed = m collect {
case (n, User(i, f, s, p)) if s == oldName =>
(n, User(i, f, newName, p))
}
(changed.size, m ++ changed)
}
surnameA2B("surName1", "Appleyard", userMap)
res1: (Int, Map[Int,User]) = (1,Map(1 -> User(1,firstName1,Appleyard,1111), 2 -> User(2,firstName2,surName2,2222), 3 -> User(3,firstName3,surName3,3333)))
Given a name to match, a replacement name and a usermap, it returns a count of the changed users and a new usermap with the changes in place. 0 means no changes, so you can derive your boolean just by testing if the count is 0 or not.
Checking for a condition, then acting if it is true, then setting a condition based on the actions is a very imperative, non-Scala way of doing things. In fact, it is rarely necessary to check if a condition is true, when you can chain functions so that A will naturally happen if B completed (and for each relevant iteration of B, if necessary. Scala makes it easy to try this approach and it results in cleaner code.