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

scala - appending to empty Seq[String] - Stack Overflow

programmeradmin4浏览0评论

I was trying to append a Seq[String] to existing an empty Seq[String]

var enrichmentColumnsList: Seq[String] = Seq.empty
datasetEnricherConf.map(u => u.columnMappingsConf).foreach {
  columnMappingConf =>
    for (i <- 0 to columnMappingConf.size) {
      var cond = columnMappingConf.map(_.destColumnName)
      println(s"cond:$cond")
      enrichmentColumnsList= cond
    }
}


println(s"enrichmentColumnsList:$enrichmentColumnsList")

cond:List(NODE)
cond:List(NODE)
cond:List(NODE_temp, NETWORK, SITETYPE, STATEORPROVINCE, DISTRICT, CLUSTER, REGION)
cond:List(NODE_temp, NETWORK, SITETYPE, STATEORPROVINCE, DISTRICT, CLUSTER, REGION)
cond:List(NODE_temp, NETWORK, SITETYPE, STATEORPROVINCE, DISTRICT, CLUSTER, REGION)
cond:List(NODE_temp, NETWORK, SITETYPE, STATEORPROVINCE, DISTRICT, CLUSTER, REGION)
cond:List(NODE_temp, NETWORK, SITETYPE, STATEORPROVINCE, DISTRICT, CLUSTER, REGION)
cond:List(NODE_temp, NETWORK, SITETYPE, STATEORPROVINCE, DISTRICT, CLUSTER, REGION)
cond:List(NODE_temp, NETWORK, SITETYPE, STATEORPROVINCE, DISTRICT, CLUSTER, REGION)
cond:List(NODE_temp, NETWORK, SITETYPE, STATEORPROVINCE, DISTRICT, CLUSTER, REGION)
enrichmentColumnsList:List(NODE_temp, NETWORK, SITETYPE, STATEORPROVINCE, DISTRICT, CLUSTER, REGION)

It initializes it with the last occurrence of the list.

I was expecting something like below:

enrichmentColumnsList:List(NODE,NODE_temp, NETWORK, SITETYPE, STATEORPROVINCE, DISTRICT, CLUSTER, REGION)

I was trying to append a Seq[String] to existing an empty Seq[String]

var enrichmentColumnsList: Seq[String] = Seq.empty
datasetEnricherConf.map(u => u.columnMappingsConf).foreach {
  columnMappingConf =>
    for (i <- 0 to columnMappingConf.size) {
      var cond = columnMappingConf.map(_.destColumnName)
      println(s"cond:$cond")
      enrichmentColumnsList= cond
    }
}


println(s"enrichmentColumnsList:$enrichmentColumnsList")

cond:List(NODE)
cond:List(NODE)
cond:List(NODE_temp, NETWORK, SITETYPE, STATEORPROVINCE, DISTRICT, CLUSTER, REGION)
cond:List(NODE_temp, NETWORK, SITETYPE, STATEORPROVINCE, DISTRICT, CLUSTER, REGION)
cond:List(NODE_temp, NETWORK, SITETYPE, STATEORPROVINCE, DISTRICT, CLUSTER, REGION)
cond:List(NODE_temp, NETWORK, SITETYPE, STATEORPROVINCE, DISTRICT, CLUSTER, REGION)
cond:List(NODE_temp, NETWORK, SITETYPE, STATEORPROVINCE, DISTRICT, CLUSTER, REGION)
cond:List(NODE_temp, NETWORK, SITETYPE, STATEORPROVINCE, DISTRICT, CLUSTER, REGION)
cond:List(NODE_temp, NETWORK, SITETYPE, STATEORPROVINCE, DISTRICT, CLUSTER, REGION)
cond:List(NODE_temp, NETWORK, SITETYPE, STATEORPROVINCE, DISTRICT, CLUSTER, REGION)
enrichmentColumnsList:List(NODE_temp, NETWORK, SITETYPE, STATEORPROVINCE, DISTRICT, CLUSTER, REGION)

It initializes it with the last occurrence of the list.

I was expecting something like below:

enrichmentColumnsList:List(NODE,NODE_temp, NETWORK, SITETYPE, STATEORPROVINCE, DISTRICT, CLUSTER, REGION)
Share Improve this question asked yesterday vikrant ranavikrant rana 4,6767 gold badges34 silver badges80 bronze badges 5
  • 1 It's quite hard to understand what you are trying to do. What is the type of datasetEnricherConf? What's the value? I would guess that you are trying to populate a an empty list iterating another list. I would recommend to read first baeldung - scala collections to understand how a collection work in scala, what are the different methods you have. I think just with map, flatMap or a for yield you can solve your problem. Please edit your post trying to clarify the input type and values, the logic you need to apply and the expected output – Gastón Schabas Commented yesterday
  • 2 It looks like what you want is datasetEnricherConf.flatMap(_.columnMappingsConf).distinct. But can't be sure without more details. – Johny T Koshy Commented yesterday
  • 2 While I recommend using flatMap and vals instead of all this imperative code, your error lies in enrichmentColumnsList= cond: you should concatenate the previous value of enrichmentColumnsList with cond before reassigning it. – Gaël J Commented yesterday
  • 2 And the right Scala way would be datasetEnricherConf.flatMap(_.columnMappingsConf).map(_.destColumnName). One line, no loop, no mutability. – Gaël J Commented yesterday
  • @Gaël J. thats cool. I get the desired output. You can post it as an answer. I will accept it. – vikrant rana Commented yesterday
Add a comment  | 

1 Answer 1

Reset to default 3

What you want can be achieved in a more functional way (that Scala encourages) using the following code:

val enrichmentColumnsList: Seq[String] =
  datasetEnricherConf
    // "flatMap" because columnMappingsConf is itself a Sequence/Iterable
    .flatMap(_.columnMappingsConf) 
    // As opposed to here where we can just use "map"
    .map(_.destColumnName)

Notice especially that we're not using mutable data (var) and no loop.

发布评论

评论列表(0)

  1. 暂无评论