Comments (14)
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.
Original comment by [email protected]
on 22 Oct 2007 at 5:12
from google-collections.
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.
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.
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.
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.
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.
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.
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.
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.
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.
oops. s/case/cast/
Original comment by [email protected]
on 1 Nov 2007 at 7:40
from google-collections.
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.
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)
- Preconditions.checkArgumentNotNull HOT 2
- MapMaker with weak keys: keySet() doubles reference counts in map HOT 4
- broken checksums in maven artefacts for rc4 HOT 6
- Predicates.equalTo() should return a Predicate<Object> HOT 2
- Make com.google.common.collect.ImmutableCollection.Builder public HOT 3
- Easy way to deal with null Iterables when looping HOT 1
- Please provide Sets.symmetricDifference() HOT 3
- static method Functions.toStringFunction() should be parametrized HOT 5
- LinkedListMultimap values should be of type Deque<V> HOT 3
- add transformEntries method to Maps and SortedMaps HOT 4
- Be consistent with [Collection]s.new*[Collection](...) HOT 3
- CusomConcurrentHashMap.Values should override toArray(T[]) HOT 2
- with/withoutmethods on immutable collections HOT 2
- OutOfMemoryError when using MapMaker and Soft/Weak-Keys/Values HOT 1
- MemoryLeak(?) when using expiration in MapMaker HOT 2
- equals is not implemented for Collections2.filter HOT 1
- Allow asMap() to return a Map which has defined value Collections HOT 1
- GWT Module missing HOT 3
- Create an alternative to commons-collections MultiKey
- Patch for /trunk/testfw/com/google/common/collect/testing/MapInterfaceTest.java
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from google-collections.