简体   繁体   English

在Scala中的地图上存储迭代器

[英]Storing iterator on a map in Scala

I need to store an iterator on a Map to implement a traversal of a trie. 我需要在Map上存储一个迭代器,以实现遍历Trie。 In Java I did: 在Java中,我做到了:

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. 我这样做的原因是,我使用队列来遍历trie,并且迭代器维护遍历的状态。

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. 在Scala中,除非我重新使用Java util.Iterator和Java util.Map,否则我不知道如何创建相应的迭代器。 Is there a more idiomatic way to do this in Scala? Scala中有更惯用的方法吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM