Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #11715

Re: Converting Sets

From Jim Janney <jjanney@shell.xmission.com>
Newsgroups comp.lang.java.programmer
Subject Re: Converting Sets
Date 2012-02-02 13:13 -0700
Organization not at this time
Message-ID <2pliol804p.fsf@shell.xmission.com> (permalink)
References <11fki7dcr12je80bqi6q7ml6f0tc78965a@4ax.com> <4f2a767d$0$449$426a34cc@news.free.fr> <vq3li79bbkel9maksor8n63m6jr5pgsq5q@4ax.com> <4f2aa4fe$0$6897$426a74cc@news.free.fr> <jgegmb$gqc$1@dont-email.me>

Show all headers | View raw


markspace <-@.> writes:

> On 2/2/2012 7:06 AM, Mayeul wrote:
>> On 02/02/2012 14:32, Roedy Green wrote:
>
>>> Ah, but this this you created is NOT a Set<Y>. You cannot add
>>> arbirary Y to it, just more X. I feel queasy. How could the compiler
>>> keep track that setOfY could only contain X.
>
>>
>> It cannot.
>
>
> I think this is covered in Effective Java.  Generics are a compile
> time thing.  If you want runtime, you have to roll your own (I don't
> know of any classes in the API that allow you to have a runtime type
> parameter, although I guess there may be some).
>
> class MySet extends SomeSet {
>
>   Class type;
>
>   MySet( Class type ) { this.type = type; }
>
>   void add( Object o ) {
>     if( o instanceof type ) super.add( o );
>   }
> }
>
> I think is the above is the gist of how EJ handles it.  Add a type
> token (the "type" variable) and test for types as they are added.  If
> you want generics too, add them in:
>
> class MySet<T> extends Set<T> {
>
>   Class<? extends T> type;
>
>   MySet( Class<? extends T> type ) { this.type = type; }
>
>   void add( T o ) {
>     if( o instanceof type ) super.add( o );
>   }
> }
>
> Not compiled, but I think that gives the right idea.

See checkedSet in java.util.Collections.

-- 
Jim Janney

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


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