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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:48:11+00:00 2026-06-04T02:48:11+00:00

I habitually use HashMap in my programs, since I know it is usually the

  • 0

I habitually use HashMap in my programs, since I know it is usually the most efficient (if properly used) and can cope with large maps easily. I know about EnumMap which is very useful for enumeration keys, but often I am generating a small map which will never get very big, is likely to be discarded pretty soon, and has no concurrency issues.

Is HashMap<K,V> too complicated for these small, local and temporary uses? Is there another, simple, implementation which I can use in these cases?

I think I’m looking for a Map implementation which is analogous to ArrayList for List. Does it exist?


Added later after responses:

Here is a scenario where a slow but very simple implementation might be better — when I have many, many of these Maps. Suppose, for example, I have a million or so of these tiny little maps, each with a handful (often less than three) of entries. I have a low reference rate — perhaps I don’t actually reference them before they are discarded most of the time. Is it still the case that HashMap is the best choice for them?

Resource utilisation is more than just speed — I would like something that doesn’t fragment the heap a lot and make GCs take a long time, for example.

It may be that HashMap is the right answer, but this is not a case of premature optimisation (or at least it may not be).


Added much later after some thought:

I decided to hand-code my own SmallMap. It is easy to make one with AbstractMap. I have also added a couple of constructors so that a SmallMap can be constructed from an existing Map.

Along the way I had to decide how to represent Entrys and to implement SmallSet for the entrySet method.

I learned a lot by coding (and unit-testing this) and want to share this, in case anyone else wants one. It is on github here.

  • 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-04T02:48:13+00:00Added an answer on June 4, 2026 at 2:48 am

    There is no standard small implementation of Map in Java. HashMap is one of the best and most flexible Map implementations around, and is hard to beat. However, in the very small requirement area — where heap usage and speed of construction is paramount — it is possible to do better.

    I have implemented SmallCollections on GitHub to demonstrate how this might be done. I would love some comments on whether I have succeeded. It is by no means certain that I have.

    Although the answers offered here were sometimes helpful, they tended, in general, to misunderstand the point. In any case, answering my own question was, in the end, much more useful to me than being given one.

    The question here has served its purpose, and that is why I have ‘answered it myself’.

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

Sidebar

Related Questions

One of the previous developers where I work habitually, consistently used: ConfigurationSettings.AppSettings[Foo].ToString() It twigs
I habitually write code with lots of functions, I find it makes it clearer.
It's been habitual in most of the scenarios while developing a database design we
The last time I heavily used C++ was years ago, and it was strictly
I'm creating a web application that will involve habitual file uploading and retrieval (PDFs,
EDIT: I understand there is keyboard-quit (which is normally bound to C-g); but I'm
This is probably habitual programming redundancy. I have noticed DECLSPEC_NOVTABLE ( __declspec(novtable) ) on
I 'm integrating a geographic coordinate class from CodePlex to my personal toolbox library.
I would like to take a picture of whats happenning to my screen, but
I have textboxes in my Windows Forms application, and I want that when the

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.