Your filter isn't actually producing any benefit if the resulting collection isn't saved for further processing.
dataMapStorage.keys
.foreach { key =>
val value = dataMapStorage.get(key)
if (currentTime.getMillis - value.getMillis > timeout) {
// ... do some work with "value"
dataMapStorage.remove(key)
}
}
Also, if dataMapStorage is just a key->value Map then couldn't you simplify it?
dataMapStorage.foreach { case (key, value) =>
if (currentTime.getMillis - value.getMillis > timeout) {
// ... do some work with "value"
dataMapStorage.remove(key)
}
}
You could use collect() here but since the procedure results in a side-effect I think foreach() is more explanatory.
Of course the real Scala FP approach is to avoid mutable data structures.
val newDataMapStorage = oldDataMapStorage.filter { case (key, value) =>
if (currentTime.getMillis - value.getMillis > timeout) {
// ... do some work with "value"
false
} else true
}