Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6337925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:20:43+00:00 2026-05-24T19:20:43+00:00

val map1 = Map(1 -> 9 , 2 -> 20) val map2 = Map(1

  • 0
val map1 = Map(1 -> 9 , 2 -> 20)
val map2 = Map(1 -> 100, 3 -> 300)

I want to merge them, and sum the values of same keys. So the result will be:

Map(2->20, 1->109, 3->300)

Now I have 2 solutions:

val list = map1.toList ++ map2.toList
val merged = list.groupBy ( _._1) .map { case (k,v) => k -> v.map(_._2).sum }

and

val merged = (map1 /: map2) { case (map, (k,v)) =>
    map + ( k -> (v + map.getOrElse(k, 0)) )
}

But I want to know if there are any better solutions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T19:20:44+00:00Added an answer on May 24, 2026 at 7:20 pm

    Scalaz has the concept of a Semigroup which captures what you want to do here, and leads to arguably the shortest/cleanest solution:

    scala> import scalaz._
    import scalaz._
    
    scala> import Scalaz._
    import Scalaz._
    
    scala> val map1 = Map(1 -> 9 , 2 -> 20)
    map1: scala.collection.immutable.Map[Int,Int] = Map(1 -> 9, 2 -> 20)
    
    scala> val map2 = Map(1 -> 100, 3 -> 300)
    map2: scala.collection.immutable.Map[Int,Int] = Map(1 -> 100, 3 -> 300)
    
    scala> map1 |+| map2
    res2: scala.collection.immutable.Map[Int,Int] = Map(1 -> 109, 3 -> 300, 2 -> 20)
    

    Specifically, the binary operator for Map[K, V] combines the keys of the maps, folding V‘s semigroup operator over any duplicate values. The standard semigroup for Int uses the addition operator, so you get the sum of values for each duplicate key.

    Edit: A little more detail, as per user482745’s request.

    Mathematically a semigroup is just a set of values, together with an operator that takes two values from that set, and produces another value from that set. So integers under addition are a semigroup, for example – the + operator combines two ints to make another int.

    You can also define a semigroup over the set of "all maps with a given key type and value type", so long as you can come up with some operation that combines two maps to produce a new one which is somehow the combination of the two inputs.

    If there are no keys that appear in both maps, this is trivial. If the same key exists in both maps, then we need to combine the two values that the key maps to. Hmm, haven’t we just described an operator which combines two entities of the same type? This is why in Scalaz a semigroup for Map[K, V] exists if and only if a Semigroup for V exists – V‘s semigroup is used to combine the values from two maps which are assigned to the same key.

    So because Int is the value type here, the "collision" on the 1 key is resolved by integer addition of the two mapped values (as that’s what Int’s semigroup operator does), hence 100 + 9. If the values had been Strings, a collision would have resulted in string concatenation of the two mapped values (again, because that’s what the semigroup operator for String does).

    (And interestingly, because string concatenation is not commutative – that is, "a" + "b" != "b" + "a" – the resulting semigroup operation isn’t either. So map1 |+| map2 is different from map2 |+| map1 in the String case, but not in the Int case.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

scala> val m = Map(1 -> 2) m: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2) scala>
def mainCaller() = { val name = xyz someList.foreach { u:Map => foo(name, u)
val name = mike val xml = <name>{name}</name> xml will be <name>mike</name> But what
val name = mike val str = Hi, {name}! println(str) I want it output
Assuming: val l1 = List(1,2,3) val l2 = List(2,3,1) I want a method that
INSERT INTO details (idactivity,user,hours,val,date) VALUES ('981','133','0','10500','2008-07-01'),('981','184','0','2750','2008-07-01'),('981','184','0','2750','2008-07-01') (iddetails as details table PK) Is there a
I have code like this: val dm = List[String]() val dk = List[Map[String,Object]]() .....
If I have this: val a = Array(...) and I write a.par.map(e => someFunc(e))
val m: java.util.Map[String, Int] = ... m.foreach { entry => val (key, value) =
I want to convert val.ElementAt(i) to float value : float *d = new float[NMAX];

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.