Working with HashMaps
HashMaps are similar to arrays, but they use a different method for accessing elements. Arrays use an integer for the index, while HashMaps use a String. HashMaps are really useful when you need to search for a specific item in a large collection of data.
How to do it...
We start by declaring a HashMap
object and adding some values to it, inside the setup()
function.
HashMap<String, Float> hm; void setup() { hm = new HashMap<String, Float>(); hm.put("Processing", 51.30); hm.put("openFrameworks", 30.45); hm.put("Cinder", 12.78); noLoop(); }
The first thing we'll do inside the draw()
function is loop through the HashMap
object using an Iterator and print each entry to the console.
Iterator i = hm.entrySet().iterator(); while ( i.hasNext () ) { Map.Entry me = (Map.Entry)i.next(); println( "Key: " + me.getKey() + ", Value: " + me.getValue() ); } println("---");
If you want to check whether a HashMap is empty, you can do that with the isEmpty()
method...