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

  • Home
  • SEARCH
  • 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 8206025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:30:38+00:00 2026-06-07T08:30:38+00:00

I have a LinkedHashMap which I’ve been using in a typical way: adding new

  • 0

I have a LinkedHashMap which I’ve been using in a typical way: adding new key-value
pairs to the end, and accessing them in order of insertion. However, now I have a
special case where I need to add pairs to the “head” of the map. I think there’s
some functionality inside the LinkedHashMap source for doing this, but it has private
accessibility.

I have a solution where I create a new map, add the pair, then add all the old mappings.
In Java syntax:

newMap.put(newKey, newValue) 
newMap.putAll(this.map)
this.map = newMap

It works. But the problem here is that I then need to make my main data structure
(this.map) a var rather than a val.

Can anyone think of a nicer solution? Note that I definitely need the fast lookup
functionality provided by a Map collection. The performance of a prepending is not
such a big deal.

More generally, as a Scala developer how hard would you fight to avoid a var
in a case like this, assuming there’s no foreseeable need for concurrency?
Would you create your own version of LinkedHashMap? Looks like a hassle frankly.

  • 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-07T08:30:40+00:00Added an answer on June 7, 2026 at 8:30 am

    This will work but is not especially nice either:

    import scala.collection.mutable.LinkedHashMap
    
    def prepend[K,V](map: LinkedHashMap[K,V], kv: (K, V)) = {
      val copy = map.toMap
      map.clear
      map += kv
      map ++= copy
    }
    
    val map = LinkedHashMap('b -> 2)
    prepend(map, 'a -> 1)
    map == LinkedHashMap('a -> 1, 'b -> 2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the first key/value pair in a LinkedHashMap, which I get from a
I have java.util.LinkedHashMap with Integer as Key and Character as Value. I know the
I have this code in Android: Map<String, String> idObject1 = new LinkedHashMap<String, String>(); idObject1.put(Id,
I have used LinkedHashMap because it is important the order in which keys entered
I have written a program which scans css files using jar cssparser-0.9.5.jar and i
I have a List of HashMap 's which has key of type Integer and
I have a LinkedHashMap<String,Object> and was wondering what the most efficient way to put
I have a map like this, Map<Integer,ArrayList<Object>> myMap = new LinkedHashMap<Integer,ArrayList<Object>>(); Now I have
I have a Map of key / values, which I initialize in @PostConstruct as
I have used LinkedHashMap<String, Integer> lhmNotFinal because it is important the order in which

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.