Hash Map for Mendix

Content Type: Module
Categories: Extensions,Data

Overview

Ever felt the need to use a hash map in Mendix? This Java based module provides a hash map for Mendix!

A hash map is an associative array or dictionary that relates unique keys to values. Hash maps are fast for in-memory lookups, especially on big data sets (>10K objects). Use cases include quickly finding an object, finding or counting duplicates, and aggregating data from an API without needing to persist it first.

This Mendix implementation (currently) only supports string-typed keys - which is the most used key anyway - and object based values. The object itself must be a specialization of the entity provided in the module. The object is extendable through specialization and contains its own key, to allow for bi-directional look-ups.

Documentation

Typical usage scenario

A hash map (or hash table) is a data structure that implements an associative array, also known as a dictionary. It relates – maps – (unique!) keys to values, where both the key and value can be primitives values but also (complex) objects. A hash map is mainly used for in-memory computations as it enables fast lookups.

Strengths of a hash map include the possibility for fast lookups, inserts and deletes – on average, a such an action is O(1) [1]: constant and thus not depending on the size of the map – and you can use almost anything as key.

Weaknesses of a hash map are that they are one-directional and unordered: if you need to find the key for a given value, or the smallest key, you have to go through the whole set – O(n): linear, depending on the size of the map.

[1] See https://en.wikipedia.org/wiki/Big_O_notation#Orders_of_common_functions for more explanation on this notation

 

Hash maps work well with large data sets – typically more than 10.000 objects – in memory, including, but not limited to:

  • Quickly finding an object by ID, without retrieving it from the database: instead of querying every single object by ID from the database, you can retrieve all objects and put them in a hash map (key=ID, value=object) and then do lookups by ID. This is mainly advantageous when you need to do a lot of lookups, as you need to build the hash map prior to using it.
  • Finding duplicates or frequencies: by using some object or string value as key in a hash map, you can easily detect whether an object was entered before, and possibly increase the frequency (stored as value) to count the occurrences of a set of words in a text. Doing this in memory with ‘normal’ lists, will be slow, especially with larger data sets.
  • Aggregating data from an API call without having to persist it before ‘querying’.

Features and limitations

While the Java implementation for Hash Map allows (almost) anything to use both for key and value, in Mendix it is harder to make it generic. Instead, this implementation supports string as key and any object as value. As most objects can be converted to a string, e.g., by hashing it or using an identifier, this shouldn’t be much of a problem. Moreover, the value object is always the provided AbstractMxHashMapEntry object (or a specialization thereof, see Usage section), with the advantage - compared to the standard hash map - that it knows its key.

The actual implementation is done in Java, and several Java actions are provided to work with the hash map from your Mendix model, similar to the function that are available for a Java Hash map:

  • Create: creates a new hash map
  • PutEntry: adds an entry to the hash map – if there was a value already present for the provided key, this action will overwrite the previous value
  • GetEntry: provides the value associated with the given key, if present
  • GetEntries: provides all values from the map
  • ContainsKey: checks whether the hash map contains the given key
  • RemoveEntry: deletes an entry from the hash map (when available)
  • Size: shows the size of the hash map (number of keys/values)
  • IsEmpty: checks whether the hash map is empty (contains no keys/values)
  • Clear: removes all keys and values from the hash map
  • Delete: completely deletes the hash map from memory

Dependencies

None

Installation

Just download and go :)

Usage

The Mendix implementation contains, next to the Java actions outlined above, two entities:

  • MxHashMap: should not be specialized nor should it be created in a microflow. Use JAVA_Hashmap_Create instead. This will return a MxHashMap object that can be used in the other Java actions to work with the hash map.
  • AbstractMxHashMapEntry: this contains by default the string key (this is currently the only type that is allowed). Create your own specialization to define the value(s); basically this specialized entity is the value (that also knows its key) and it can be a list itself, by referring it to other entities.

To allow for multiple hash maps in memory, the actual implementation (in Java) uses a nested hash map: the first hash map uses the Mendix object GUID as key and the actual (Java) hash map as value.

Known bugs

None - so far :)

Releases

Version: 1.0.0
Framework Version: 10.18.5
Release Notes: First release