最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

In Scala, how would I update specific items in a Seq using a filter? - Stack Overflow

programmeradmin3浏览0评论

I have a Seq of objects (protobuf objects). I would like to iterate through the list, where a specific field value is filtered, and update these specific objects in the original list.

For example:

val serversProto = Seq(...)

serversProto = serversProto
  .filter(server => server.registrationStatus == "REGISTERED")
  .map(server => 
    ... update server object
  )

The problem with the above is that serverProtos is now a filtered list ONLY containing items that matched the filter. What I want to do is update the items in serversProto where the filter is true, but keep all the other original items.

Is this possible?

I have a Seq of objects (protobuf objects). I would like to iterate through the list, where a specific field value is filtered, and update these specific objects in the original list.

For example:

val serversProto = Seq(...)

serversProto = serversProto
  .filter(server => server.registrationStatus == "REGISTERED")
  .map(server => 
    ... update server object
  )

The problem with the above is that serverProtos is now a filtered list ONLY containing items that matched the filter. What I want to do is update the items in serversProto where the filter is true, but keep all the other original items.

Is this possible?

Share Improve this question edited Jan 19 at 9:24 Gaël J 15.1k5 gold badges22 silver badges43 bronze badges asked Jan 18 at 23:33 Kris RiceKris Rice 8451 gold badge10 silver badges28 bronze badges 4
  • 2 Why not map with if-else and modify only items that match the predicate? – Mateusz Kubuszok Commented Jan 18 at 23:41
  • @MateuszKubuszok did not think of that, thanks, that worked – Kris Rice Commented Jan 18 at 23:45
  • @MateuszKubuszok please post as an answer with an example – Kris Rice Commented Jan 18 at 23:45
  • Why will a filter pipeline keep things which do not map filter. Your question should be "How do I update only items which match a filter" ? – sarveshseri Commented Jan 19 at 7:01
Add a comment  | 

1 Answer 1

Reset to default 0

There is a few option, one of the option is more easy to understand, like Mateusz Kubuszok told above.

  1. Using map with IF - ELSE,
    val withMap = serversProto.map(server => 
      if (server.registrationStatus == "REGISTERED") {
        server.copy(name = s"${server.name}-updated")
      } else {
        server
      }
    )
  1. Using map with Pattern Matching
    val withPatternMatch = serversProto.map {
      case server if server.registrationStatus == "REGISTERED" =>
        server.copy(name = s"${server.name}-updated")
      case server => server
    }
  1. Using collect
    val withCollect = serversProto.collect {
      case server if server.registrationStatus == "REGISTERED" =>
        server.copy(name = s"${server.name}-updated")
      case server => server
    }

But I recommend using no. 3, because its more clean and simple and more idiomatic in scala

发布评论

评论列表(0)

  1. 暂无评论