Coder Social home page Coder Social logo

hashmap's Introduction

hashmap

This is an implementation of a HashMap. The constructor for the HashMap class takes in an integer to specify the number of buckets in its array. The HashMap class contains the following methods:

  • hash(value): Takes a value and produces a hash code with it.
  • set(key, value): Adds a new key-value pair to the HashMap. If a key already exists, then the old value is overwritten.
  • get(key): Returns the value that is assigned to this key in the HashMap. If key is not found, returns null.
  • has(key): Returns true or false based on whether or not the key is in the HashMap.
  • remove(key): Removes the given key from the HashMap. If it does not exist or does not match the bucket it's led to, throws an error.
  • length(): Returns the number of stored keys in the HashMap.
  • clear(): Removes all entries in the HashMap.
  • keys(): Returns an array containing all the keys inside the HashMap.
  • values(): Returns an array containing all the values.
  • entries(): Returns an array that contains each key-value pair. Example: [[firstKey, firstValue], [secondKey, secondValue]]

The following utility method assists any main methods that iterate over the HashMap:

  • indexError(index): Throws an error when a given index is outside the bounds of the size range for HashMap's buckets array. This emulates errors thrown in other programming languages where arrays cannot be dynamically sized.

Lessons Learned

  • Using hash functions to sort key-value pairs into buckets.
  • As a second layer of security, confirming that a given key matches the key in a bucket it has been hashed to.

hashmap's People

Contributors

ken-ux avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.