Is iterator supported by processing?

I hope to use hashmap in Processing, and I hope to use an iterator to traverse all entries in hashmap. However, when I hope to use an iterator, they tell me that "Cannnot find a class or type named Iterator." Part of the code is shown below.

Iterator i = nodeTable.entrySet().iterator(); // Get an iterator while (i.hasNext()) { Node nodeDisplay = (Node)i.next(); nodeDisplay.drawSelf(); } 

From the processing site http://processing.org/reference/HashMap.html I know that an iterator can be used to move a hash map. Howvere, I can not find more information about the iterator. I am wondering if iterator is supported in processing? Or do I need to import some libraries so that I can use them?

+4
source share
2 answers

While the problem is resolved, I put part of my code here if someone else comes across this. Thanks again for your kind help.

 import java.util.Iterator; // Import the class of Iterator // Class definition and the setup() function are omitted for simplicity // The iterator is used here HashMap<String, Node> nodeTable = new HashMap<String, Node>(); void draw(){ // Part of this function is omitted Iterator<Node> i = nodeTable.values().iterator(); // Here I use the iterator to get the nodes stored the hashtable and I use the function values() here. entrySet() or keySet() can also be used when necessary while (i.hasNext()) { Node nodeDisplay = (Node)i.next(); // Now you can use the node from the hashmap } } 
+2
source

Glad you solved your problem, but for others entrySet() across this, if you want to iterate over entrySet() , there are two ways to do this. The first way to do this is:

 import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; public class Testing { public static void main(String[] args) { Map<String, String> strMap = new HashMap<String, String>(); strMap.put("foo", "bar"); strMap.put("alpha", "beta"); for (Iterator<Entry<String, String>> iter = strMap.entrySet().iterator(); iter.hasNext(); ) { Entry<String, String> entry = iter.next(); System.out.println(entry.getKey() + "=" + entry.getValue()); } } } 

Note the import at the top of the code; you probably don't see it for Iterator .

And the second:

 import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; public class Testing { public static void main(String[] args) { Map<String, String> strMap = new HashMap<String, String>(); strMap.put("foo", "bar"); strMap.put("alpha", "beta"); for (Entry<String, String> entry : strMap.entrySet()) System.out.println(entry.getKey() + "=" + entry.getValue()); } } 

This is called for each loop and eliminates the need to use Iterator in general and makes the code much simpler. Note that this can also be used for arrays to remove the need for an index:

 String[] strs = {"foo", "bar"}; for (String str : strs) System.out.println(str); 
+1
source

All Articles