Coder Social home page Coder Social logo

Comments (14)

GoogleCodeExporter avatar GoogleCodeExporter commented on August 29, 2024
Thanks for the suggestion. It would be nice if that was feasible. 
Unfortunately, when
I tried implementing that method signature, it made asMap() more difficult to 
use.

The main usage of asMap() is when people iterate across 
multimap.asMap().entrySet().
When asMap returned Map<K, ? extends Collection<V>>, the application code 
reported
confusing "capture of ?" compilation errors. The caller could work around that 
with
some ugly casting, but leaving the code as-is is cleaner.

Another possibility just occurred to me. We could add the following method to
ListMultimap (and similar methods to SetMultimap and SortedSetMultimap):
  Map<K, List<V>> asListMap()

That provides the functionality you want -- avoiding casts in the application 
code --
without the messiness of generic wildcards.

Original comment by [email protected] on 22 Oct 2007 at 5:12

from google-collections.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 29, 2024

Original comment by [email protected] on 22 Oct 2007 at 5:12

from google-collections.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 29, 2024
asListMap() feels like such a hack (but might be better than nothing).

It's very sad that this change really seemed to overwhelm javac's ability to 
make
sense of the generics.  Jared, can you give us an example of what some typical 
client
code would have to look like (before vs. after) if this change were made?

Original comment by [email protected] on 22 Oct 2007 at 5:44

from google-collections.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 29, 2024
Here's a typical usage of asMap():

  public static void printValueSizes(Multimap<String, Integer> multimap) {
    Map<String, Collection<Integer>> map = multimap.asMap();
    Set<Map.Entry<String, Collection<Integer>>> es = map.entrySet();
    for (Map.Entry<String, Collection<Integer>> entry : es) {
      String key = entry.getKey();
      Collection<Integer> values = entry.getValue();
      System.out.println(key + " has " + values.size() + " values");
    }
  }

If we change the Multimap.asMap() signature to
  Map<K, ? extends Collection<V>> asMap();

I couldn't find a clean way to adjust that iteration logic. Here's the best I 
could
come up with:

  public static void printValueSizes(Multimap<String, Integer> multimap) {
    Map<String, ? extends Collection<Integer>> map = multimap.asMap();
    Set<?> es = map.entrySet();
    for (Object object : es) {
      @SuppressWarnings("unchecked")
      Map.Entry<String, ? extends Collection<Integer>> entry =
        (Map.Entry<String, ? extends Collection<Integer>>) object;
      String key = entry.getKey()
      Collection<Integer> values = entry.getValue();
      System.out.println(key + " has " + values.size() + " values");
    }
  }

That's really ugly, but nothing else compiles. Unless a generics guru has a 
better
idea, asListMap() is the best we can do.

Original comment by [email protected] on 1 Nov 2007 at 5:43

from google-collections.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 29, 2024
what am i missing?  i'm not using multimap, just a simple map, but this code 
compiles
and runs fine on my box (jdk 1.5.0_12-b04):

[code]
  public static void main(String[] args) throws Exception
  {
    Map<String, List<Integer>> tmp =
      new HashMap<String, List<Integer>>();
    tmp.put("foo", Arrays.asList(42));

    Map<String, ? extends Collection<Integer>> map = tmp;
    for(Map.Entry<String, ? extends Collection<Integer>> e : map.entrySet()) {
      System.out.println("Value " + e.getValue());
    }

  }
[/code]

Original comment by [email protected] on 1 Nov 2007 at 1:56

from google-collections.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 29, 2024
jahlborn, the issue here has to do with Multimap.  I don't see the connection 
between
the issue and this code you pasted.

Original comment by [email protected] on 1 Nov 2007 at 4:18

from google-collections.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 29, 2024
jahlborn's example is relevant. The iterator does work, though you can't define 
the
entry set variable as an intermediate step.

  public static void printValueSizes(Multimap2<String, Integer> multimap) {
    Map<String, ? extends Collection<Integer>> map = multimap.asMap();
// WON'T COMPILE:   Set<Map.Entry<String, ? extends Collection<Integer>>> es =
map.entrySet();
    for (Map.Entry<String, ? extends Collection<Integer>> entry : map.entrySet()) {
      String key = entry.getKey();
      Collection<Integer> values = entry.getValue();
      System.out.println(key + " has " + values.size() + " values");
    }
  }

We have the option of changing the method signature and iterating across the 
entry
set, while being unable to store the entry set in a separate variable. I'm not
convinced that's an improvement, but it's plausible.

Original comment by [email protected] on 1 Nov 2007 at 4:36

from google-collections.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 29, 2024
Ok, it's probably just too early in the morning for me.

My main concern is that we don't make regular usage patterns by regular folks in
regular situations more confusing/ugly/bulky.

Original comment by [email protected] on 1 Nov 2007 at 4:55

from google-collections.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 29, 2024
That's why I'm leaning toward adding methods like ListMultimap.asListMap(). In 
my
experience, methods that return generics with "?" are difficult to use.

Original comment by [email protected] on 1 Nov 2007 at 6:30

from google-collections.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 29, 2024
It is also conceivable that we just do nothing and clients have to cast.

Clearly our library wants you to never have to cast, and that's good.  But 
there may
be a level of diminishing returns at some point in that effort.

Original comment by [email protected] on 1 Nov 2007 at 7:19

from google-collections.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 29, 2024
Ah, now i see your concern.  In order to hold the intermediate set, you need:

Set<? extends Map.Entry<String, ? extends Collection<Integer>>> eSet = 
map.entrySet();

which is a bit on the ugly side.  however, to kevin's point about making this 
code
useable, i would argue most people do not hold the intermediate entrySet, but 
iterate
over it as in my example.  and, if they do that, then they do not need to case 
the
e.getValue() result which is *very* nice.

Original comment by [email protected] on 1 Nov 2007 at 7:39

from google-collections.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 29, 2024
oops.  s/case/cast/

Original comment by [email protected] on 1 Nov 2007 at 7:40

from google-collections.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 29, 2024
FYI, another similar issue to keep an eye on:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6231312

Original comment by [email protected] on 1 Nov 2007 at 9:56

from google-collections.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 29, 2024
After considering the alternatives, I've concluded that maintaining the status 
quo is
the best choice. Adding "? extends" to the interface makes it more difficult to 
use.
A manual cast seems better than creating methods like asListMap().

Original comment by [email protected] on 3 Nov 2007 at 12:24

  • Changed state: WontFix

from google-collections.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.