I need to store an iterator on a Map to implement a traversal of a trie. In Java I did:
class Node {
HashMap<Character, Node> children = new HashMap<>();
Iterator<Character> i = children.keySet().iterator();
public boolean hasNext() {
return i.hasNext();
}
public CharNode next() {
Character letter = i.next();
return new CharNode(letter, children.get(letter));
}
...
The reason I am doing this is that I use a queue to traverse the trie, and the iterators maintain the state of the traversal.
In Scala, I couldn't figure out how to create the corresponding iterator though, unless I fall back on Java util.Iterator, and Java util.Map. Is there a more idiomatic way to do this in Scala?
HashMap in Scala also supports iterator()
http://www.scala-lang.org/api/current/#scala.collection.immutable.HashMap
http://www.scala-lang.org/api/current/#scala.collection.Iterator
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.