This reference is for Processing 3.0+. If you have a previous version, use the reference included with your software in the Help menu. If you see any errors or have suggestions, please let us know. If you prefer a more technical reference, visit the Processing Core Javadoc and Libraries Javadoc.

Name

HashMap

Examples
import java.util.Map;

// Note the HashMap's "key" is a String and "value" is an Integer
HashMap<String,Integer> hm = new HashMap<String,Integer>();

// Putting key-value pairs in the HashMap
hm.put("Ava", 1);
hm.put("Cait", 35);
hm.put("Casey", 36);

// Using an enhanced loop to iterate over each entry
for (Map.Entry me : hm.entrySet()) {
  print(me.getKey() + " is ");
  println(me.getValue());
}

// We can also access values by their key
int val = hm.get("Casey");
println("Casey is " + val);

Description A HashMap stores a collection of objects, each referenced by a key. This is similar to an Array, only instead of accessing elements with a numeric index, a String is used. (If you are familiar with associative arrays from other languages, this is the same idea.) The above example covers basic use, but there's a more extensive example included with the Processing examples. In addition, for simple pairings of Strings and integers, Strings and floats, or Strings and Strings, you can now use the simpler IntDict, FloatDict, and StringDict classes.

For a list of the numerous HashMap features, please read the Java reference description.
Constructor
HashMap<Key, Value>()
HashMap<Key, Value>(initialCapacity)
HashMap<Key, Value>(initialCapacity, loadFactor)
HashMap<Key, Value>(m)
Parameters
Key Class Name: the data type for the HashMap's keys
Value Class Name: the data type for the HashMap's values
initialCapacity int: defines the initial capacity of the map; the default is 16
loadFactor float: the load factor for the map; the default is 0.75
m Map: gives the new HashMap the same mappings as this Map
RelatedIntDict
FloatDict
StringDict
Updated on January 21, 2019 10:05:16am EST

Creative Commons License