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

The Archive Base Latest Questions

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

Would a hashtable/hashmap use a lot of memory if it only consists of object

  • 0

Would a hashtable/hashmap use a lot of memory if it only consists of object references and int’s?

As for a school project we had to map a database to objects (that’s what being done by orm/hibernate nowadays) but eager to find a good way not to store id’s in objects in order to save them again we thought of putting all objects we created in a hashmap/hashtable, so we could easily retrieve it’s ID. My question is if it would cost me performance using this, in my opinion more elegant way to solve this problem.

  • 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-14T21:49:20+00:00Added an answer on May 14, 2026 at 9:49 pm

    Would a hashtable/hashmap use a lot of
    memory if it only consists of object
    references and int’s?

    “a lot” depends on how many objects you have. For a few hundreds or a few thousands, you’re not going to notice.

    But typically the default Java collections are really incredibly inefficient when you’re working with primitives (because of the constant boxing/unboxing from “primitive to wrapper” going on, like say “int to Integer”) , both from a performances and memory standpoint (the two being related but not identical).

    If you have a lot of entries, like hundreds of thousands or millions, I suggest using for example the Trove collections.

    In your case, you’d use this:

    TIntObjectHashMap<SomeJavaClass>
    

    or this:

    TObjectIntHashMap<SomeJavaClass>
    

    In any case, that shall run around circle the default Java collections perf-wise and cpu-wise (and it shall trigger way less GC, etc.).

    You’re dodging the unnecessary automatic (un)boxing from/to int/Integer, the collections are creating way less garbage, resizing in a much smarter way, etc.

    Don’t even get me started on the default Java HashMap<Integer,Integer> compared to Trove’s TIntIntHashMap or I’ll go berzerk 😉

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

Sidebar

Related Questions

I would like to know how to use a System.Collections.Hashtable in F#. The reason
I have a java progam in which there are a lot of HashMap/HashTable being
Can I use a Regular Expression with Hashtable.containsKey(Object value); Is there any way to
I would like to know if by default is Hashtable passed by reference or
How would I go about writing a Makefile that would call, say, gedit hashtable.cpp
If I have many queues and each has an unique ID, would a Hashtable
My Hashtable in Java would benefit from a value having a tuple structure. What
I would like to use JNDI to look up Kerberos SRV records in a
I have a synchronized Hashtable with int as the key, and a custom class
Is there any reason that I would be missing the java.util.HashMap package? I have

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.