如何为 TreeMap 和 HashMap (Java) 创建可迭代的包装器?

发布于 2024-08-17 19:03:13 字数 558 浏览 4 评论 0原文

我有一个包装 TreeMap 的 MyMap 类。 (假设它是狗的集合,并且键是字符串)。

public class MyMap {
   private TreeMap<String, Dog> map;
...
}

我想使用 for-each 循环将 MyMap 变成可迭代的。我知道如果我的类是 LinkedList 包装器,我会怎么做:

public class MyList implements Iterable<Dog> {
   private LinkedList<Dog> list;
   ...
   public Iterator<Dog> iterator() {
      return list.iterator();
   }
}

但是这样的解决方案不适用于 TreeMap,因为 TreeMap 没有 iterator()。那么如何使 MyMap 可迭代呢?

同样的问题,除了 MyMap 包装了 HashMap(而不是 TreeMap)。

谢谢。

I have a class MyMap which wraps TreeMap.
(Say it's a collection of dogs and that the keys are strings).

public class MyMap {
   private TreeMap<String, Dog> map;
...
}

I would like to turn MyMap iterable with the for-each loop. I know how I would've done it if my class was a LinkedList wrapper:

public class MyList implements Iterable<Dog> {
   private LinkedList<Dog> list;
   ...
   public Iterator<Dog> iterator() {
      return list.iterator();
   }
}

But such a solution doesn't work for TreeMap because TreeMap doesn't have an iterator(). So how can I make MyMap iterable?

And the same question except MyMap wraps HashMap (instead of TreeMap).

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

趴在窗边数星星i 2024-08-24 19:03:13
public Iterator<Dog> iterator() {
      return map.values().iterator();
}
public Iterator<Dog> iterator() {
      return map.values().iterator();
}
仙女山的月亮 2024-08-24 19:03:13

这是因为您只能迭代 Map 的键或值,而不能迭代 Map 本身。

通常您可以这样做:

for( Object value : mymap.values()  ){
  System.out.println(value);
}

那么,我的建议是:您的 Map 是否需要有一个可迭代对象?如果您只想获取值...或键本身,则不是。

另外,请考虑使用 Google 的转发集合,例如 转发列表

It's because you can only iterate the keys or the values of a Map, not the map itself

Typically you can do this:

for( Object value : mymap.values()  ){
  System.out.println(value);
}

So, what I'm suggesting is: does your Map need to have an iterable? Not if you just want to get at the values... or the keys themselves.

Also, consider using Google's forwarding collections such as ForwardingList

提赋 2024-08-24 19:03:13
public class MyMap implements Iterable<Dog> {
   private TreeMap<String, Dog> map;
   ...
   @Override
   public Iterator<Dog> iterator() {
      return map.values().iterator();
   }
}

map.values() 是地图中包含的狗的集合视图。集合的迭代器将按照相应键在树中出现的顺序返回值。
感谢乔纳森·范伯格。

public class MyMap implements Iterable<Dog> {
   private TreeMap<String, Dog> map;
   ...
   @Override
   public Iterator<Dog> iterator() {
      return map.values().iterator();
   }
}

map.values() is a collection view of the dogs contained in map. The collection's iterator will return the values in the order that their corresponding keys appear in the tree.
Thanks to Jonathan Feinberg.

作业与我同在 2024-08-24 19:03:13

一种可能是定义一个 entrySet () 方法返回一个 Set,然后迭代该 Set。

For-each 迭代看起来像这样:

for (Map.Entry<String,Integer> m: someMap.entrySet()){
   System.out.println("Key="+m.getKey()+" value="+m.getValue());
}

One possibility may be to define an entrySet() method that returns a Set and then iterate over the Set.

For-each iteration would look something like this:

for (Map.Entry<String,Integer> m: someMap.entrySet()){
   System.out.println("Key="+m.getKey()+" value="+m.getValue());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文