TimurMahammadov / google-collections

Automatically exported from code.google.com/p/google-collections
Apache License 2.0
0 stars 0 forks source link

Iterable maps #90

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
It should be simple to have an interface (and accompanying implementations)
that have the same semantics as org.apache.commons.collections.IterableMap,
and org.apache.commons.collections.MapIterator. Obviously, iterable maps in
google collections should be genericized.

The simplest implementation would enhance the JDK's HashMap, and this would
address the vast majority of use cases for an iterable map. The more
difficult ones would involve enhancements to MultiMap implementations, as
well as ConcurrentMap implementations.

Original issue reported on code.google.com by alan.kt....@gmail.com on 8 Jul 2008 at 3:07

GoogleCodeExporter commented 9 years ago
Ok, I've looked up these Apache things. I think you're asking for this:

BEFORE:

  for (Map.Entry<String, Integer> entry : map.entrySet()) {
    doSomethingWith(entry.getKey(), entry.getValue());
  }

AFTER:

  MapIterator<String, Integer> it = Maps.iterate(map);
  while (it.hasNext()) {
    doSomethingWith(it.getKey(), it.getValue());
  }

Have I got that right?

Original comment by kevin...@gmail.com on 8 Jul 2008 at 3:49

GoogleCodeExporter commented 9 years ago
Plus I think you want an IterableMap interface that extends Map with an 
iterator()
method, so "Maps.iterate(map)" in the example above would become 
"map.iterator()".

How much of the motivation for this is supposed performance gains?

Original comment by kevin...@gmail.com on 8 Jul 2008 at 4:08

GoogleCodeExporter commented 9 years ago
It's not clear what benefit MapIterator provides over iterating across the 
entrySet()
directly. Besides, with Java 5+, it's better to use the improved for loops 
instead of
iterators most of the time.

Now, if you prefer to use a MapIterator, it would be straightforward to write a
method that creates a MapIterator for an arbitrary Map. Such a method would 
belong in
the Apache Commons Collections library, not of Google Collections.

Original comment by jared.l....@gmail.com on 12 Jul 2008 at 1:32