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 9234473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:47:52+00:00 2026-06-18T06:47:52+00:00

Consider the following code that counts the frequency of each string in the list

  • 0

Consider the following code that counts the frequency of each string in the list and stores the results in the mutable map. This works great, but I don’t understand where the += method is defined?! Is this some weird implicit conversion thing or what? I saw this code somewhere but it didn’t include an explanation for the +=.

val list = List("a", "b", "a")
val counts = new scala.collection.mutable.HashMap[String, Int]().withDefaultValue(0)
list.foreach(counts(_) += 1)
counts
//> res7: scala.collection.mutable.Map[String,Int] = Map(a -> 2, b -> 1)

The apply of map returns an Int, but Int doesn’t have a += and this method updates the map with a new value, so it looks as if the apply returns a mutable integer that has a += method…

  • 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-06-18T06:47:53+00:00Added an answer on June 18, 2026 at 6:47 am

    This is not an implicit conversion – it is a desugaring.
    Writing:

    x += 1
    

    desugars to:

    x = x + 1
    

    if the class of x does not have a += method defined on it.

    In the same way:

    counts("a") += 1
    

    desugars to:

    counts("a") = counts("a") + 1
    

    because counts("a") is an Int, and Int does not have a += method defined.

    On the other hand, writing:

    x(expression1) = expression2
    

    desugars to a call to the update method in Scala:

    x.update(expression1, expression2)
    

    Every mutable Map has an update method defined – it allows setting keys in the map.

    So the entire expression is desugared to:

    list.foreach(x => counts.update(x, counts(x) + 1))
    

    This += is not to be confused with the += method on mutable.Maps in Scala. That method updates the entry in the map if that key already existed, or adds a new key-value pair. It returns the this reference, that is, the same map, so you can chain += calls. See ScalaDoc or the source code.

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

Sidebar

Related Questions

Consider the following code and assume that list is an synchronized List. List list
Consider the following code that automates generation of Boost.MPL type sequences (list or vector).
Consider this following code (that retrieves the response from a HTTP request and prints
Consider following code: Dim S1 As String = a 'this is the string in
Consider the following code. It's an anchor tag with an id called leader-module-total that
Consider the following code: int i = 3 << 65; I would expect that
Consider the following code: while(true) { someFunction(); Thread.sleep(1000); } What I want is that,
Consider the following code: function Dog() { this.foo = function() {}; this.walk = function()
This is rather interesting, I think. Consider following code, both the window.onload and body
Consider the following code that uses JSch to create an SSH connection: public class

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.