users@jsr311.java.net

Re: JAX-RS: generics: <String,Object> vs <String,?>

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Wed, 16 Jul 2008 08:36:02 -0400

On Jul 16, 2008, at 2:56 AM, Manger, James H wrote:

> One method in UriBuilder is
> build(Map<String,Object> values);
>
> I think it should be
> build(Map<String,?> values);
> so callers can pass, say, a Map<String,String> to the method – which
> is the most likely type they will be using.
>
> build(…) only reads the Map so it should be fine with <String,?>.
>
>
> I am not totally confident about this, because I am not totally
> confident about generics and I seem to be able to pass a
> Map<String,String> to the Jersey (0.9) implementation of build()!
>
Either works. Strings are Objects so Map<String, String> is assignable
to Map<String, Object>. You can think of Map<String, Object> as
equivalent to Map<String, ? extends Object>, the compiler treats them
the same IIRC. I prefer the Map<String, Object> since a user can
actually instantiate one - you can't, e.g. write Map<String, ?> m =
new HashMap<String, ?>().

Marc.

---
Marc Hadley <marc.hadley at sun.com>
CTO Office, Sun Microsystems.