JAM - Just Another Map
Our very own Map type
You can go nuts with Maps and we will partially do that.
What are the minimum Map requirements?
A map is a data structure and it is mainly used for fast look ups. It stores data in the form of key and value pairs where every key is unique. Each key here maps to a value and hence the name map.
The idea is to use a mathematical functional to derive a numeric value (the hash value) from the key and use it to map the key to a storage position.
We get back to our training shortly...
A short computer science detour.
A hash function is any function that can be used to map data of arbitrary size to data of fixed size. The values returned by a hash function are called hash values, hash codes, digests, or simply hashes.
https://en.wikipedia.org/wiki/Hash_function
Java delivers by default hashes for every non-primitive data type.
int hashCode()
is part of Object
which is the base
for every non-primitive data type
hashCode()
is basic and context-less
equals(Object)
method, then the hash code must be identical as well.
Partially taken from java.lang.Object.hashCode()
Two examples
/**
* Returns a hash code for this string. The hash code for a
* {@code String} object is computed as
*
* s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
*
* using {@code int} arithmetic, where {@code s[i]} is the
* ith character of the string, {@code n} is the length of
* the string, and {@code ^} indicates exponentiation.
* (The hash value of the empty string is zero.)
*
* @return a hash code value for this object.
*/
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
/**
* Returns a hash code for a {@code boolean} value; compatible with
* {@code Boolean.hashCode()}.
*
* @param value the value to hash
* @return a hash code value for a {@code boolean} value.
* @since 1.8
*/
public static int hashCode(boolean value) {
return value ? 1231 : 1237;
}
We will discuss oddities later when we start to create our own objects.
All stuff needs names first
Keep in mind, if something is used by someone else, you cannot fix naming easily anymore.
public interface SimpleMap<K, V>
{
public V get(K);
public V put(K, V);
public void putAll(SimpleMap<K, V>);
public V remove(K);
public int size();
public void clear();
public boolean contains(K)
}
/**
* Interface for map-like data structures that behave similarly
* to known implementations such as HashMap.
* <p>
* This interface is not to be confused with java.util.Map despite being
* similar it is not fully comparable due to the lack of method coverage.
*
* @author René Schwietzke
*/
public interface SimpleMap<K, V>
{
/**
* Returns the stored value of a key if it exists, null otherwise
*
* @param key the key to look for
* @returns the found value or null if not found
*/
public V get(K key);
}
Let's build the interface.