|
| 1 | +# Java Collections For JavaScript |
| 2 | + |
| 3 | +This repository contains implementations of some commonly-used Java collections in JavaScript. |
| 4 | + |
| 5 | +## HashMap |
| 6 | + |
| 7 | +[HashMap.js](src/HashMap.js) implements a [Hash Table/Map](https://en.wikipedia.org/wiki/Hash_table) with similar methods to [Java's HashMap](https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html). |
| 8 | +All objects (except primitives) used as keys must implement a <code>hashCode</code> method to return an integer hash code as well as an <code>equals</code> method that accepts another object as a parameter and returns whether or not the value of the current object is equal to parameter. Objects that are considered equal must have the same hash code, but the converse is not true. <br> |
| 9 | +Note that <code>HashMap</code> instances are iterable, so <code>[...myHashMap]</code> will return an array of arrays, where each inner array consists of the key and value for a mapping (in that order). |
| 10 | + |
| 11 | +### Constructor |
| 12 | + |
| 13 | +The constructor accepts an optional <code>HashMap</code>, which will copy the contents. Alternately, it will create an empty <code>HashMap</code>. |
| 14 | + |
| 15 | +``` |
| 16 | +const myMap = new HashMap(); // create empty HashMap |
| 17 | +const myMap2 = new HashMap(myMap); // create HashMap from other HashMap |
| 18 | +``` |
| 19 | + |
| 20 | +### Instance Methods |
| 21 | + |
| 22 | +<table> |
| 23 | + <tr><th>Method Signature</th><th>Description</th></tr> |
| 24 | + <tr> |
| 25 | + <td><code>put(key, value)</code></td><td>Associates the given <code>key</code> with the <code>value</code>. Returns the previous value associated with the key, or <code>undefined</code> if there was no value before.</td> |
| 26 | + </tr> |
| 27 | + <tr> |
| 28 | + <td><code>putIfAbsent(key, value)</code></td><td>Associates the given <code>key</code> with the <code>value</code> only if there was no value previously associated with the key.</td> |
| 29 | + </tr> |
| 30 | + <tr> |
| 31 | + <td><code>get(key)</code></td><td>Returns the value associated with the given <code>key</code> or <code>undefined</code> if there was no value.</td> |
| 32 | + </tr> |
| 33 | + <tr> |
| 34 | + <td><code>getOrDefault(key, defaultValue)</code></td><td>Returns the value associated with the given <code>key</code> if there is one or <code>defaultValue</code> if not.</td> |
| 35 | + </tr> |
| 36 | + <tr> |
| 37 | + <td><code>containsKey(key)</code></td><td>Returns <code>true</code> if the <code>HashMap</code> contains a mapping for the given <code>key</code>.</td> |
| 38 | + </tr> |
| 39 | + <tr> |
| 40 | + <td><code>containsValue(value)</code></td><td>Returns <code>true</code> if there is at least one key mapped to the given <code>value</code>.</td> |
| 41 | + </tr> |
| 42 | + <tr> |
| 43 | + <td><code>clear()<code></td><td>Removes all mappings in the <code>HashMap</code>.</td> |
| 44 | + </tr> |
| 45 | + <tr> |
| 46 | + <td><code>remove(key)</code></td><td>Removes the mapping for the given <code>key</code> and returns its value.</td> |
| 47 | + </tr> |
| 48 | + <tr> |
| 49 | + <td><code>forEach(consumer)</code></td><td>Iterates over each key-value mapping in the <code>HashMap</code> and passes the key and value of each one as arguments to the <code>consumer</code> callback function.</td> |
| 50 | + </tr> |
| 51 | + <tr> |
| 52 | + <td><code>isEmpty()</code></td><td>Returns <code>true</code> if there are no mappings in the <code>HashMap</code></td> |
| 53 | + </tr> |
| 54 | + <tr> |
| 55 | + <td><code>size()</code></td><td>Returns the number of mappings in the <code>HashMap</code>.</td> |
| 56 | + </tr> |
| 57 | + <tr> |
| 58 | + <td><code>hashCode()</code></td><td>Returns an integer hash code for this instance.</td> |
| 59 | + </tr> |
| 60 | + <tr> |
| 61 | + <td><code>equals(obj)</code></td><td>Returns <code>true</code> if the value of <code>obj</code> is equal to the value of this instance.</td> |
| 62 | + </tr> |
| 63 | +</table> |
0 commit comments