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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:09:06+00:00 2026-06-12T10:09:06+00:00

We have code with two complementary Maps like this: private final Map<Integer, String> idToName

  • 0

We have code with two complementary Maps like this:

private final Map<Integer, String> idToName = new HashMap<Integer, String>();
private final Map<String, Integer> nameToID = new TreeMap<String, Integer>();

Whenever we put something in one, we also put in the other (with the key and value reversed) like this:

nameToID.put(name, id);
idToName.put(id, name);

We’re running into a memory problem with this application. It seems like there is a lot of duplication here. Is there a way to make this more memory-efficient? Some single structure that we could use? I realize that this might be at the cost of time-efficiency, so I’m interested in what the trade-offs would be.

  • 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-12T10:09:08+00:00Added an answer on June 12, 2026 at 10:09 am

    This is exactly what Guava’s BiMap does, though there’s only so much added memory efficiency you can get. The biggest advantage of BiMap isn’t so much memory efficiency as “it takes care of ensuring values are unique, and you can’t forget to update the inverse map.”

    BiMap<Integer, String> idToName = HashBiMap.create();
    idToName.put(1, "foo");
    idToName.inverse(); // returns a BiMap mapping "foo" to 1
    idToName.inverse().put("bar", 2); // idToName now has an extra mapping 2 -> "bar"
    

    (Disclosure: I contribute to Guava.)

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

Sidebar

Related Questions

I have this code to identify two different image map shape attributes. What I'm
I have code like this: public class test { public string aa { get;
I have this code (two classes) class url { private $profile_id; private $short; public
I have a code like <span ><img src="xxx.png">Tab1</span><span ><img src="yyy.png">Tab2</span> This code creates two
I have found this code in two posts here: String url = content://sms/; Uri
I have two actionlinks on my MVC view like this: <%= Html.ActionLink (click me,Partial1,Temp,new
Say I have a class Code defined like this, with a user specified type
I have code like: var entityX = this._xService.GetAll(); var entityY = this._yService.GetAll(); Both are
So i have this code that creates two windows: WNDCLASS wc; wc.style = CS_BYTEALIGNCLIENT
I have this code to control two check boxes (buttons customized): - (IBAction) setCheckBox:

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.