PC Review Forums Newsgroups Microsoft DotNet Microsoft VB .NET Dictionary with keys only. Anything ?

Reply

Dictionary with keys only. Anything ?

 
Thread Tools Rate Thread
Old 13-02-2007, 03:03 AM   #1
pamela fluente
Guest
 
Posts: n/a
Default Dictionary with keys only. Anything ?



Sometimes I define a
System.Collections.Generic.Dictionary(Of Key, ...)

but actually I only use the Key part. Is there any collection which I
can use
which is similar to a Dictionary, but without the value part ?
I mean just a list of unique key Object , with a O(1) retrieval
operation ?

-Pam

  Reply With Quote
Old 13-02-2007, 03:50 AM   #2
Michael D. Ober
Guest
 
Posts: n/a
Default Re: Dictionary with keys only. Anything ?

All collections have value parts. If your keys are strings, use
System.Collections.Specialized.StringCollection. Otherwise, try

dim Valueless as new Dictionary(of KeyType, KeyType)
Valueless.add(key, key)

At least this way you don't have two objects floating around memory - just
one with two references.

Mike Ober.

"pamela fluente" <pamelafluente@libero.it> wrote in message
news:1171335790.959069.236090@v45g2000cwv.googlegroups.com...
>
> Sometimes I define a
> System.Collections.Generic.Dictionary(Of Key, ...)
>
> but actually I only use the Key part. Is there any collection which I
> can use
> which is similar to a Dictionary, but without the value part ?
> I mean just a list of unique key Object , with a O(1) retrieval
> operation ?
>
> -Pam
>
>




  Reply With Quote
Old 13-02-2007, 05:45 AM   #3
Branco Medeiros
Guest
 
Posts: n/a
Default Re: Dictionary with keys only. Anything ?

On Feb 13, 12:03 am, "pamela fluente" <pamelaflue...@libero.it> wrote:
> Sometimes I define a
> System.Collections.Generic.Dictionary(Of Key, ...)
>
> but actually I only use the Key part. Is there any collection which I
> can use
> which is similar to a Dictionary, but without the value part ?
> I mean just a list of unique key Object , with a O(1) retrieval
> operation ?
>
> -Pam


I'm particularly fond of
System.Collections.ObjectModel.KeyedCollection(Of Key, Item). It's
usefull when the item is also the key, or the key is a property of the
item itself. Unfortunately its an abstract class, which means you must
inherit it, because there's a GetKeyForItem method that needs
implementation. Indexed access is O(1), while keyed access is near
O(1) -- or so the docs say. Besides, it has the definite advantage (to
me) that it keeps the insertion order (which a Dictionary won't).

HTH.

Regards,

Branco.




  Reply With Quote
Old 13-02-2007, 11:39 AM   #4
pamela fluente
Guest
 
Posts: n/a
Default Re: Dictionary with keys only. Anything ?

On 13 Feb, 04:50, "Michael D. Ober" <obermd.@.alum.mit.edu.nospam>
wrote:
> All collections have value parts. If your keys are strings, use
> System.Collections.Specialized.StringCollection.


The times I need keys only are often those where the object is also
used as a key (through an appropriate comparer).

Otherwise, try
>
> dim Valueless as new Dictionary(of KeyType, KeyType)
> Valueless.add(key, key)


Yes that's what I am doing. Or else I do Valueless.add(key, Nothing)
(is this better?) which anyway seems quite awkward to me.

>
> At least this way you don't have two objects floating around memory - just
> one with two references.
>
> Mike Ober.
>


  Reply With Quote
Old 13-02-2007, 11:41 AM   #5
pamela fluente
Guest
 
Posts: n/a
Default Re: Dictionary with keys only. Anything ?

On 13 Feb, 06:45, "Branco Medeiros" <branco.medei...@gmail.com> wrote:
> On Feb 13, 12:03 am, "pamela fluente" <pamelaflue...@libero.it> wrote:
>
> > Sometimes I define a
> > System.Collections.Generic.Dictionary(Of Key, ...)

>
> > but actually I only use the Key part. Is there any collection which I
> > can use
> > which is similar to a Dictionary, but without the value part ?
> > I mean just a list of unique key Object , with a O(1) retrieval
> > operation ?

>
> > -Pam

>
> I'm particularly fond of
> System.Collections.ObjectModel.KeyedCollection(Of Key, Item). It's
> usefull when the item is also the key, or the key is a property of the
> item itself. Unfortunately its an abstract class, which means you must
> inherit it, because there's a GetKeyForItem method that needs
> implementation. Indexed access is O(1), while keyed access is near
> O(1) -- or so the docs say. Besides, it has the definite advantage (to
> me) that it keeps the insertion order (which a Dictionary won't).


Hmm I might give it a try. Isn't strange that there isn't a collection
of keys only ? I think it could be nice to have it

>
> HTH.
>
> Regards,
>
> Branco.



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off