Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #11726
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | markspace <-@.> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Converting Sets |
| Date | Fri, 03 Feb 2012 14:25:50 -0800 |
| Organization | A noiseless patient Spider |
| Lines | 39 |
| Message-ID | <jghmtg$o0o$1@dont-email.me> (permalink) |
| References | <11fki7dcr12je80bqi6q7ml6f0tc78965a@4ax.com> <4f2a767d$0$449$426a34cc@news.free.fr> <qahmi71a48dtupjn43n9cabt1vuhie65sq@4ax.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| Injection-Date | Fri, 3 Feb 2012 22:25:52 +0000 (UTC) |
| Injection-Info | mx04.eternal-september.org; posting-host="XjIWM99mD7Ijfdu600oVPA"; logging-data="24600"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+5U+vzrQtCtI30F2DdImPDrRbeuwqs0IE=" |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0) Gecko/20120129 Thunderbird/10.0 |
| In-Reply-To | <qahmi71a48dtupjn43n9cabt1vuhie65sq@4ax.com> |
| Cancel-Lock | sha1:enZCnxvbbSWxYcQryrpr5H6r/yY= |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:11726 |
Show key headers only | View raw
On 2/2/2012 6:25 PM, Roedy Green wrote:
> thanks. I have written an SSCCE to explain this in tedious detail.
> See http://mindprod.com/jgloss/generics.html#CONVERTINGSETS
From your example,
static void showDogs2( Set<? extends Dog> dogs )
Is the correct solution. This is exactly what wildcards were designed
for. You should learn the acronym that Joshua Bloch invented as a
mnemonic: PECS - Producer Extends, Consumer Super.
In this case, showDogs2 is a producer in the sense that within the
method body, the variable "dogs" produces dogs for output, but the
method body doesn't put any dogs into the set, or modify the set in anyway.
This
Set<? extends Dog> someDogs2 = someDalmatians;
Is wrong, in the sense that it's too hamstrung to be useful. As your
comments indicate, it's hard to work with, and doesn't really do what
you want either. As a corollary, this
static Set<? super Dog> makeSomeDogs() {...
would be broken for the same reason. You almost never want to return a
type with a wildcard. Just give the user an single type, it will do
what you wanted much better than the wildcard version.
static Set<Dog> makeSomeDogs() {...
Finally, where do you use the super wild card then? Something like this:
static void copy(Set<? extends Dog> from, Set<? super Dog> to) {...
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread
Converting Sets Roedy Green <see_website@mindprod.com.invalid> - 2012-02-01 23:34 -0800
Re: Converting Sets Mayeul <mayeul.marguet@free.fr> - 2012-02-02 12:47 +0100
Re: Converting Sets Roedy Green <see_website@mindprod.com.invalid> - 2012-02-02 05:32 -0800
Re: Converting Sets Mayeul <mayeul.marguet@free.fr> - 2012-02-02 16:06 +0100
Re: Converting Sets markspace <-@.> - 2012-02-02 09:21 -0800
Re: Converting Sets Jim Janney <jjanney@shell.xmission.com> - 2012-02-02 13:13 -0700
Re: Converting Sets markspace <-@.> - 2012-02-02 13:46 -0800
Re: Converting Sets Roedy Green <see_website@mindprod.com.invalid> - 2012-02-02 16:29 -0800
Re: Converting Sets Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-02-02 17:34 -0800
Re: Converting Sets Arne Vajhøj <arne@vajhoej.dk> - 2012-02-04 20:49 -0500
Re: Converting Sets Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-02-06 12:55 -0800
Re: Converting Sets Arne Vajhøj <arne@vajhoej.dk> - 2012-02-06 20:47 -0500
Re: Converting Sets Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-02-06 17:51 -0800
Re: Converting Sets Arne Vajhøj <arne@vajhoej.dk> - 2012-02-06 20:57 -0500
Re: Converting Sets markspace <-@.> - 2012-02-03 14:12 -0800
Re: Converting Sets Lew <lewbloch@gmail.com> - 2012-02-03 22:32 -0800
Re: Converting Sets Lew <lewbloch@gmail.com> - 2012-02-03 22:30 -0800
Re: Converting Sets Lew <lewbloch@gmail.com> - 2012-02-03 22:33 -0800
Re: Converting Sets Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-02-02 11:02 -0800
Re: Converting Sets Roedy Green <see_website@mindprod.com.invalid> - 2012-02-02 18:25 -0800
Re: Converting Sets markspace <-@.> - 2012-02-03 14:25 -0800
csiph-web