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 Answer
Reset to default 3What 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.
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 withmap
,flatMap
or afor 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 yesterdaydatasetEnricherConf.flatMap(_.columnMappingsConf).distinct
. But can't be sure without more details. – Johny T Koshy Commented yesterdayflatMap
andval
s instead of all this imperative code, your error lies inenrichmentColumnsList= cond
: you should concatenate the previous value ofenrichmentColumnsList
withcond
before reassigning it. – Gaël J Commented yesterdaydatasetEnricherConf.flatMap(_.columnMappingsConf).map(_.destColumnName)
. One line, no loop, no mutability. – Gaël J Commented yesterday