HashMap

Data / Composite

Description

A templated key-value map. In C++ Mode, std::map and std::unordered_map from the standard library are often more idiomatic, but HashMap is available for Processing Java compatibility.

Syntax

HashMap<std::string, int> map map.put("key", 1) int v = map.get("key")

Parameters

None

Returns

HashMap

Methods

put(K k, V v)Inserts or updates a key-value pair
get(K k)Returns reference to value for key
containsKey(K k)Returns true if key exists
remove(K k)Removes a key-value pair
size()Returns number of entries
isEmpty()Returns true if empty
clear()Removes all entries
keySet()Returns a vector of all keys
values()Returns a vector of all values

Under the Hood

From Processing.h:

template<typename K, typename V> class HashMap { public: std::map<K,V> data; void put(const K& k, const V& v) { data[k]=v; } V& get(const K& k) { return data[k]; } bool containsKey(const K& k) const { return data.count(k)>0; } bool containsValue(const V& v) const { for(auto& p:data) if(p.second==v) return true; return false; } void remove(const K& k) { data.erase(k); } int size() const { return (int)data.size(); } bool isEmpty() const { return data.empty(); } void clear() { data.clear(); } std::vector<K> keySet() const { std::vector<K> r; for(auto& p:data) r.push_back(p.first); return r; } std::vector<V> values() const { std::vector<V> r; for(auto& p:data) r.push_back(p.second); return r; } V& operator[](const K& k) { return data[k]; } };