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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:27:34+00:00 2026-06-13T03:27:34+00:00

For simplicity’s sake, let’s say I have two instances of HashMap<String, String> where they

  • 0

For simplicity’s sake, let’s say I have two instances of HashMap<String, String> where they share the same keys. What I want to know is there a performance and memory difference between that and representing those two String values in an Object and storing them in HashMap<String, Object>.

My actual problem uses an instance of HashMap<String, HashSet<String>> and two instances of HashMap<String, Double> and I was hoping that by consolidating them, I’d save memory somehow, but I wasn’t sure if there’d be a performance impact from using self-defined Object versus a native object like HashSet or Double as values.

  • 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-13T03:27:35+00:00Added an answer on June 13, 2026 at 3:27 am

    The hashes are calculated from the strings, so there won’t be a speed impact. The space impact (a very slight increase) will be negligible in the long run. If it makes the code more legible, do it and screw the performance (as long as we’re not talking huge performance bottlenecks, it doesn’t matter).

    Speed

    For the speed impact, remember that in a HashMap<String, ?>, the String is what gets hashed. You may see a small increase in speed, actually, since you’ll only have to do one lookup to find your custom object compared to 3 lookups.

    Space

    For space impact, remember that HashMap uses an inner array that’s the size of a power of 2. If you’re using just a vanilla HashMap with no special settings like a custom load factor, then you may see a slight space increase because right now you have (roughly, of course, this is just simplified):

    HashSet<String>[]
    Double[]
    Double[]
    

    And after combining it, you’ll have

    CustomObject[]
        HashSet<String>
        Double
        Double
    

    This is ignoring constant-size information that doesn’t grow with the map. Objects take up more space than just the references to their fields, but not very much.

    Legibility

    The custom object option wins this one hands-down. It’s way cleaner and is very OOP, fitting into Java very, very well. Regardless of the performance, you should do this anyway. It’ll look better in the long run and be more maintainable.

    For example, if you wanted to add fields to the custom object, that’s easy. But having the separate maps means creating more maps for more variables, which is dirty. I say go the OOP way.

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

Sidebar

Related Questions

For the sake of simplicity, let's say my database has two tables, videos and
For simplicity, let's say I want to do implement a function which takes two
For simplicity let's say I have code similar to this: def testMethod(String txt) {
For simplicity's sake, let's say we have this rather contrived table: [ID] [Weekday] [Weather]
For simplicity let's say I have two classes: a class called Car and a
For the sake of simplicity, let's say I have a Person class in Python.
For simplicity, let's say that I have two sets of words, sorted into alphabetical
For the sake of simplicity, lets say I have a CustomerViewModel and I want
For simplicity's sake, let's say I have a SQL Server CE table called Widgets
For the sake of simplicity let's say I only have 2 models: Book, Author

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.