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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:02:14+00:00 2026-05-27T21:02:14+00:00

I’m have a map that looks like this: Map[ A -> Collection[B]] . This

  • 0

I’m have a map that looks like this: Map[ A -> Collection[B]]. This map gets updated in a loop – the special thing is however, that updates mostly just mean adding an element B to the Collection[B] (for some key A).

I am trying to find out if I can get some speedup by changing the type of my Collection from List[ ] to ListBuffer[ ].

Up to now my code looked like this (simplified):

var incoming = new HashMap[A, List[B]() {
  override def default(a: A) = List()
}
..
for(b < someCollectionOfBs){
  ..
  incoming(b.getA) = b :: incoming(b.getA)
  ..
}

This works fine. Now, I changed the type of the map so it looks like this:

var incoming = new collection.mutable.HashMap[A, ListBuffer[B]() {
  override def default(a: A) = collection.mutable.ListBuffer()
}
..
for(b < someCollectionOfBs){
  ..
  incoming(b.getA) += b
  ..
}

Note the change in how the element B is added to the collection in the 2nd example (no more immutable List, hence we do not need to create and assign new collection…).

But. This does not work: incoming(X) += .. does not update the value of the map for X, actually it does not change anything.

What am I missing here? I thought that I should be able to update the values of a mutable HashMap… So, if my values are mutable collections, why can’t I just add elements to those?

  • 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-27T21:02:15+00:00Added an answer on May 27, 2026 at 9:02 pm

    The default is returned when the key is not found, but it does not update the map with the default value. You can use getOrElseUpdate for that.

    incoming.getOrElseUpdate(b.getA, ListBuffer()) += b
    

    That should do what you want.

    Additional note:

    If you’re concerned about performance, I don’t think replacing List with ListBuffer will buy you much, because you are prepending to a List and that should be very fast. ListBuffer is handy when you want to append to a list. You should look at using java.util.HashMap and see if that helps.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need a function that will clean a strings' special characters. I do NOT
I am trying to loop through a bunch of documents I have to put

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.