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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:36:38+00:00 2026-05-10T20:36:38+00:00

Yes, I know you could use regular objects as associative arrays in JavaScript, but

  • 0

Yes, I know you could use regular objects as associative arrays in JavaScript, but I’d like to use something closer to java’s Map’s implementation (HashMap, LinkedHashMap etc). Something that could have any kind of data used as key. Are there any good hash(code/table) in JavaScript implementation out there?

  • 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. 2026-05-10T20:36:38+00:00Added an answer on May 10, 2026 at 8:36 pm

    In javascript, objects are literally a hash implementation. A Java HashMap will be a little bit of a fake-out, so I’d challenge you to re-think your needs.

    The straight answer is no, I don’t believe that there is a great implementation of Java’s HashMap in javascript. If there is, it’s bound to be part of a library that you may or may not want to use, and you certainly don’t need to include a library just to have a little hash table.

    So let’s go ahead and write one, just to examine the problem. You can use it if you like. We’ll just start by writing a constructor, and we’ll piggyback off of Array, which is Object, but has some useful methods that will keep this example from getting too tedious:

    function HashMap () {     var obj = [];     return obj; }  var myHashMap = HashMap(); 

    We’ll add some methods straight from the world of Java, but translate into javascript as we go…

    function HashMap() {     var obj = [];     obj.size = function () {         return this.length;     };     obj.isEmpty = function () {         return this.length === 0;     };     obj.containsKey = function (key) {         for (var i = 0; i < this.length; i++) {             if (this[i].key === key) {                 return i;             }         }         return -1;     };     obj.get = function (key) {         var index = this.containsKey(key);         if (index > -1) {             return this[index].value;         }     };     obj.put = function (key, value) {         if (this.containsKey(key) !== -1) {             return this.get(key);         }         this.push({'key': key, 'value': value});     };     obj.clear = function () {         this = null;  // Just kidding...     };     return obj; } 

    We could continue to build it out, but I think it’s the wrong approach. At the end of the day, we end up using what javascript provides behind the scenes, because we just simply don’t have the HashMap type. In the process of pretending, it lends itself to all kinds of extra work.

    It’s a bit ironic that one of the things that makes javascript such an interesting and diverse language is the ease with which it handles this kind of wrestling. We can literally do anything we’d like, and the quick example here does nothing if it doesn’t illustrate the deceptive power of the language. Yet given that power, it seems best not to use it.

    I just think javascript wants to be lighter. My personal recommendation is that you re-examine the problem before you try implement a proper Java HashMap. Javascript neither wants nor affords for one.

    Remember the native alternative:

    var map = [{}, 'string', 4, {}]; 

    ..so fast and easy by comparison.

    On the other hand, I don’t believe that there are any hard-and-fast answers here. This implementation really may be a perfectly acceptable solution. If you feel you can use it, I’d say give it a whirl. But I’d never use it if I felt that we have reasonably simpler and more natural means at our disposal.. which I’m almost certain that we do.

    Sidenote: Is efficiency related to style? Notice the performance hit.. there’s a big O staring us in the face at HashMap.put()… The less-than-optimal performance probably isn’t a show-stopper here, and you’d probably need to be doing something very ambitious or have a large set of data before you’d even notice a performance hickup a modern browser. It’s just interesting to note that operations tend to become less efficient when you’re working against the grain, almost as if there is a natural entropy at work. Javascript is a high level language, and should offer efficient solutions when we keep in line with its conventions, just as a HashMap in Java will be a much more natural and high performing choice.

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

Sidebar

Ask A Question

Stats

  • Questions 210k
  • Answers 210k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This sounds pretty self-explanatory to me. The class questions.dao.hibernate.Questions does… May 12, 2026 at 9:56 pm
  • Editorial Team
    Editorial Team added an answer There's certainly at least one alternative to gcc on Linux:… May 12, 2026 at 9:55 pm
  • Editorial Team
    Editorial Team added an answer In the simple case you describe i'd use this $… May 12, 2026 at 9:55 pm

Related Questions

I'm rolling my own logger class, and want to represent the heirarchy of logs
Questions #1 through #4 on the Joel Test in my opinion are all about
I was recently reading this document which lists a number of strategies that could
I know many people who use computers every day, who do not know how

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.