Add stream values to existing Map in Scala
I have a Map with some calculated values and during reading from Stream, I want to put some of it's (key, values) to this map based on some condition.
How could I achieve it with a brief and concise code?
I'm thinking something like this:
var newcards = scala.collection.mutable.Map[String, String]()
var allCards = scala.collection.mutable.Map[String, String]()
...
newCards.filter(some_condition).append((k:String, v:String) => allCards.put(k, v))
// append method is not present
scala dictionary scala-collections
|
show 1 more comment
I have a Map with some calculated values and during reading from Stream, I want to put some of it's (key, values) to this map based on some condition.
How could I achieve it with a brief and concise code?
I'm thinking something like this:
var newcards = scala.collection.mutable.Map[String, String]()
var allCards = scala.collection.mutable.Map[String, String]()
...
newCards.filter(some_condition).append((k:String, v:String) => allCards.put(k, v))
// append method is not present
scala dictionary scala-collections
1
allCards = allCards ++ newcards.filter(condition)
Values under duplicate keys will be overwritten, and you really shouldn't be using mutable variables.
– jwvh
Jan 2 at 7:50
you want to add some key, valule pair or only values for the specific key?
– Raman Mishra
Jan 2 at 7:57
@RamanMishra add key+value
– Alex Stamper
Jan 2 at 18:04
@jwvh Thanks! I was exactly looking for some one-liner. is there any way to do the same in a chained call (not with assignment), just "collect into" semantics?
– Alex Stamper
Jan 2 at 18:09
1
@AlexStamper; You mean something like this:newcards.filter(condition).foreach((allCards.update _).tupled)
? (Ugh, all these mutables. I have to go wash my hands.)
– jwvh
Jan 2 at 18:55
|
show 1 more comment
I have a Map with some calculated values and during reading from Stream, I want to put some of it's (key, values) to this map based on some condition.
How could I achieve it with a brief and concise code?
I'm thinking something like this:
var newcards = scala.collection.mutable.Map[String, String]()
var allCards = scala.collection.mutable.Map[String, String]()
...
newCards.filter(some_condition).append((k:String, v:String) => allCards.put(k, v))
// append method is not present
scala dictionary scala-collections
I have a Map with some calculated values and during reading from Stream, I want to put some of it's (key, values) to this map based on some condition.
How could I achieve it with a brief and concise code?
I'm thinking something like this:
var newcards = scala.collection.mutable.Map[String, String]()
var allCards = scala.collection.mutable.Map[String, String]()
...
newCards.filter(some_condition).append((k:String, v:String) => allCards.put(k, v))
// append method is not present
scala dictionary scala-collections
scala dictionary scala-collections
edited Jan 2 at 18:11
Alex Stamper
asked Jan 2 at 7:42
Alex StamperAlex Stamper
1,10911232
1,10911232
1
allCards = allCards ++ newcards.filter(condition)
Values under duplicate keys will be overwritten, and you really shouldn't be using mutable variables.
– jwvh
Jan 2 at 7:50
you want to add some key, valule pair or only values for the specific key?
– Raman Mishra
Jan 2 at 7:57
@RamanMishra add key+value
– Alex Stamper
Jan 2 at 18:04
@jwvh Thanks! I was exactly looking for some one-liner. is there any way to do the same in a chained call (not with assignment), just "collect into" semantics?
– Alex Stamper
Jan 2 at 18:09
1
@AlexStamper; You mean something like this:newcards.filter(condition).foreach((allCards.update _).tupled)
? (Ugh, all these mutables. I have to go wash my hands.)
– jwvh
Jan 2 at 18:55
|
show 1 more comment
1
allCards = allCards ++ newcards.filter(condition)
Values under duplicate keys will be overwritten, and you really shouldn't be using mutable variables.
– jwvh
Jan 2 at 7:50
you want to add some key, valule pair or only values for the specific key?
– Raman Mishra
Jan 2 at 7:57
@RamanMishra add key+value
– Alex Stamper
Jan 2 at 18:04
@jwvh Thanks! I was exactly looking for some one-liner. is there any way to do the same in a chained call (not with assignment), just "collect into" semantics?
– Alex Stamper
Jan 2 at 18:09
1
@AlexStamper; You mean something like this:newcards.filter(condition).foreach((allCards.update _).tupled)
? (Ugh, all these mutables. I have to go wash my hands.)
– jwvh
Jan 2 at 18:55
1
1
allCards = allCards ++ newcards.filter(condition)
Values under duplicate keys will be overwritten, and you really shouldn't be using mutable variables.– jwvh
Jan 2 at 7:50
allCards = allCards ++ newcards.filter(condition)
Values under duplicate keys will be overwritten, and you really shouldn't be using mutable variables.– jwvh
Jan 2 at 7:50
you want to add some key, valule pair or only values for the specific key?
– Raman Mishra
Jan 2 at 7:57
you want to add some key, valule pair or only values for the specific key?
– Raman Mishra
Jan 2 at 7:57
@RamanMishra add key+value
– Alex Stamper
Jan 2 at 18:04
@RamanMishra add key+value
– Alex Stamper
Jan 2 at 18:04
@jwvh Thanks! I was exactly looking for some one-liner. is there any way to do the same in a chained call (not with assignment), just "collect into" semantics?
– Alex Stamper
Jan 2 at 18:09
@jwvh Thanks! I was exactly looking for some one-liner. is there any way to do the same in a chained call (not with assignment), just "collect into" semantics?
– Alex Stamper
Jan 2 at 18:09
1
1
@AlexStamper; You mean something like this:
newcards.filter(condition).foreach((allCards.update _).tupled)
? (Ugh, all these mutables. I have to go wash my hands.)– jwvh
Jan 2 at 18:55
@AlexStamper; You mean something like this:
newcards.filter(condition).foreach((allCards.update _).tupled)
? (Ugh, all these mutables. I have to go wash my hands.)– jwvh
Jan 2 at 18:55
|
show 1 more comment
2 Answers
2
active
oldest
votes
Here's one way to do it with immutable maps. I don't know what your streams look like, but maybe this can be changed to fit your needs:
object Example {
def update[K, V](m: Map[K, V])(s: Stream[(K, V)]): Map[K, V] = s match {
case (k, v) #:: kvs => update(m.updated(k, v))(kvs)
case _ => m
}
def main(args: Array[String]): Unit = {
val s1 = Stream(("b", "2"), ("c", "3"))
val s2 = Stream(("a", "100"))
val m = Map("a" -> "1")
println(update(m)(s1)) //appends
println(update(m)(s2)) //replaces
}
}
Main prints out the following:
Map(a -> 1, b -> 2, c -> 3)
Map(a -> 100)
add a comment |
You don't need to use mutable Map. If you have no other option. Prefer using TrieMap. It is concurrent and will let you modify the Map.
scala> import scala.collection.concurrent.TrieMap
import scala.collection.concurrent.TrieMap
scala> val m1= TrieMap[Int, String](1 -> "I", 2 -> "am", 3 -> "TrieMap", 4 -> "Let", 5 -> "you", 6 -> "modify")
m1: scala.collection.concurrent.TrieMap[Int,String] = TrieMap(1 -> I, 5 -> you, 2 -> am, 6 -> modify, 3 -> TrieMap, 4 -> Let)
scala> val m2= TrieMap[String, String]()
m2: scala.collection.concurrent.TrieMap[String,String] = TrieMap()
scala> m1.filter(_._1 % 2 == 0).foreach {
| case (key, value) => m2(key.toString) = value
| }
scala> m2
res1: scala.collection.concurrent.TrieMap[String,String] = TrieMap(4 -> Let, 2 -> am, 6 -> modify)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002836%2fadd-stream-values-to-existing-map-in-scala%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here's one way to do it with immutable maps. I don't know what your streams look like, but maybe this can be changed to fit your needs:
object Example {
def update[K, V](m: Map[K, V])(s: Stream[(K, V)]): Map[K, V] = s match {
case (k, v) #:: kvs => update(m.updated(k, v))(kvs)
case _ => m
}
def main(args: Array[String]): Unit = {
val s1 = Stream(("b", "2"), ("c", "3"))
val s2 = Stream(("a", "100"))
val m = Map("a" -> "1")
println(update(m)(s1)) //appends
println(update(m)(s2)) //replaces
}
}
Main prints out the following:
Map(a -> 1, b -> 2, c -> 3)
Map(a -> 100)
add a comment |
Here's one way to do it with immutable maps. I don't know what your streams look like, but maybe this can be changed to fit your needs:
object Example {
def update[K, V](m: Map[K, V])(s: Stream[(K, V)]): Map[K, V] = s match {
case (k, v) #:: kvs => update(m.updated(k, v))(kvs)
case _ => m
}
def main(args: Array[String]): Unit = {
val s1 = Stream(("b", "2"), ("c", "3"))
val s2 = Stream(("a", "100"))
val m = Map("a" -> "1")
println(update(m)(s1)) //appends
println(update(m)(s2)) //replaces
}
}
Main prints out the following:
Map(a -> 1, b -> 2, c -> 3)
Map(a -> 100)
add a comment |
Here's one way to do it with immutable maps. I don't know what your streams look like, but maybe this can be changed to fit your needs:
object Example {
def update[K, V](m: Map[K, V])(s: Stream[(K, V)]): Map[K, V] = s match {
case (k, v) #:: kvs => update(m.updated(k, v))(kvs)
case _ => m
}
def main(args: Array[String]): Unit = {
val s1 = Stream(("b", "2"), ("c", "3"))
val s2 = Stream(("a", "100"))
val m = Map("a" -> "1")
println(update(m)(s1)) //appends
println(update(m)(s2)) //replaces
}
}
Main prints out the following:
Map(a -> 1, b -> 2, c -> 3)
Map(a -> 100)
Here's one way to do it with immutable maps. I don't know what your streams look like, but maybe this can be changed to fit your needs:
object Example {
def update[K, V](m: Map[K, V])(s: Stream[(K, V)]): Map[K, V] = s match {
case (k, v) #:: kvs => update(m.updated(k, v))(kvs)
case _ => m
}
def main(args: Array[String]): Unit = {
val s1 = Stream(("b", "2"), ("c", "3"))
val s2 = Stream(("a", "100"))
val m = Map("a" -> "1")
println(update(m)(s1)) //appends
println(update(m)(s2)) //replaces
}
}
Main prints out the following:
Map(a -> 1, b -> 2, c -> 3)
Map(a -> 100)
edited Jan 2 at 15:15
answered Jan 2 at 15:09
codenoodlecodenoodle
407210
407210
add a comment |
add a comment |
You don't need to use mutable Map. If you have no other option. Prefer using TrieMap. It is concurrent and will let you modify the Map.
scala> import scala.collection.concurrent.TrieMap
import scala.collection.concurrent.TrieMap
scala> val m1= TrieMap[Int, String](1 -> "I", 2 -> "am", 3 -> "TrieMap", 4 -> "Let", 5 -> "you", 6 -> "modify")
m1: scala.collection.concurrent.TrieMap[Int,String] = TrieMap(1 -> I, 5 -> you, 2 -> am, 6 -> modify, 3 -> TrieMap, 4 -> Let)
scala> val m2= TrieMap[String, String]()
m2: scala.collection.concurrent.TrieMap[String,String] = TrieMap()
scala> m1.filter(_._1 % 2 == 0).foreach {
| case (key, value) => m2(key.toString) = value
| }
scala> m2
res1: scala.collection.concurrent.TrieMap[String,String] = TrieMap(4 -> Let, 2 -> am, 6 -> modify)
add a comment |
You don't need to use mutable Map. If you have no other option. Prefer using TrieMap. It is concurrent and will let you modify the Map.
scala> import scala.collection.concurrent.TrieMap
import scala.collection.concurrent.TrieMap
scala> val m1= TrieMap[Int, String](1 -> "I", 2 -> "am", 3 -> "TrieMap", 4 -> "Let", 5 -> "you", 6 -> "modify")
m1: scala.collection.concurrent.TrieMap[Int,String] = TrieMap(1 -> I, 5 -> you, 2 -> am, 6 -> modify, 3 -> TrieMap, 4 -> Let)
scala> val m2= TrieMap[String, String]()
m2: scala.collection.concurrent.TrieMap[String,String] = TrieMap()
scala> m1.filter(_._1 % 2 == 0).foreach {
| case (key, value) => m2(key.toString) = value
| }
scala> m2
res1: scala.collection.concurrent.TrieMap[String,String] = TrieMap(4 -> Let, 2 -> am, 6 -> modify)
add a comment |
You don't need to use mutable Map. If you have no other option. Prefer using TrieMap. It is concurrent and will let you modify the Map.
scala> import scala.collection.concurrent.TrieMap
import scala.collection.concurrent.TrieMap
scala> val m1= TrieMap[Int, String](1 -> "I", 2 -> "am", 3 -> "TrieMap", 4 -> "Let", 5 -> "you", 6 -> "modify")
m1: scala.collection.concurrent.TrieMap[Int,String] = TrieMap(1 -> I, 5 -> you, 2 -> am, 6 -> modify, 3 -> TrieMap, 4 -> Let)
scala> val m2= TrieMap[String, String]()
m2: scala.collection.concurrent.TrieMap[String,String] = TrieMap()
scala> m1.filter(_._1 % 2 == 0).foreach {
| case (key, value) => m2(key.toString) = value
| }
scala> m2
res1: scala.collection.concurrent.TrieMap[String,String] = TrieMap(4 -> Let, 2 -> am, 6 -> modify)
You don't need to use mutable Map. If you have no other option. Prefer using TrieMap. It is concurrent and will let you modify the Map.
scala> import scala.collection.concurrent.TrieMap
import scala.collection.concurrent.TrieMap
scala> val m1= TrieMap[Int, String](1 -> "I", 2 -> "am", 3 -> "TrieMap", 4 -> "Let", 5 -> "you", 6 -> "modify")
m1: scala.collection.concurrent.TrieMap[Int,String] = TrieMap(1 -> I, 5 -> you, 2 -> am, 6 -> modify, 3 -> TrieMap, 4 -> Let)
scala> val m2= TrieMap[String, String]()
m2: scala.collection.concurrent.TrieMap[String,String] = TrieMap()
scala> m1.filter(_._1 % 2 == 0).foreach {
| case (key, value) => m2(key.toString) = value
| }
scala> m2
res1: scala.collection.concurrent.TrieMap[String,String] = TrieMap(4 -> Let, 2 -> am, 6 -> modify)
answered Jan 2 at 7:59
Mahesh Chand KandpalMahesh Chand Kandpal
1,725718
1,725718
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002836%2fadd-stream-values-to-existing-map-in-scala%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
allCards = allCards ++ newcards.filter(condition)
Values under duplicate keys will be overwritten, and you really shouldn't be using mutable variables.– jwvh
Jan 2 at 7:50
you want to add some key, valule pair or only values for the specific key?
– Raman Mishra
Jan 2 at 7:57
@RamanMishra add key+value
– Alex Stamper
Jan 2 at 18:04
@jwvh Thanks! I was exactly looking for some one-liner. is there any way to do the same in a chained call (not with assignment), just "collect into" semantics?
– Alex Stamper
Jan 2 at 18:09
1
@AlexStamper; You mean something like this:
newcards.filter(condition).foreach((allCards.update _).tupled)
? (Ugh, all these mutables. I have to go wash my hands.)– jwvh
Jan 2 at 18:55