473,320 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Dictionary or List?

I want to store small collections of objects that have a unique id
string:"Name", so I opted to use a Dictionary<string,MyObject>
collection. However, since my collection will rarely exceed 10 or 12
items, would I be better of just using a regular List<MyObjectand
looping through the Names to find an object?

I know it's probably splitting hairs from a performance standpoint,
but I'm curious from a theoretical standpoint.

Jordan

Apr 9 '07 #1
3 14863
Jordan,

I'm glad that you see that this is probably a premature optimization
issue. However, that doesn't mean that you can't discuss it in a
theoretical sense.

Personally, choosing a data structure when I am initially coding a
solution isn't about the number of items that will be in the data structure,
but rather, what I need the data structure to do. A list and a dictionary
are two different things.

A dictionary is used when you need to perform lookups using some sort of
key. A list is used when you have a set (I use this term loosely, not in
the strict mathematical sense) of items. These items could possibly have a
key to be looked up on, but the list isn't concerned about that, it's really
just a simple container.

That being said, because you have a specific need for looking up an
object based on a key, I would day that the proper choice is the dictionary.

Of course, after you code and you start doing performance evaluations,
if you find that this is a major bottleneck, then yes, I would try other
solutions, but at this level, I would take clear, concise, understandable
and easily maintained code over code that is micro-optimized.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jordan" <jn****@hotmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
>I want to store small collections of objects that have a unique id
string:"Name", so I opted to use a Dictionary<string,MyObject>
collection. However, since my collection will rarely exceed 10 or 12
items, would I be better of just using a regular List<MyObjectand
looping through the Names to find an object?

I know it's probably splitting hairs from a performance standpoint,
but I'm curious from a theoretical standpoint.

Jordan

Apr 9 '07 #2
Personally, that is "splitting hairs" to me. Use the right tool for the job,
which is this case is clearly the Dictionary.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Jordan" wrote:
I want to store small collections of objects that have a unique id
string:"Name", so I opted to use a Dictionary<string,MyObject>
collection. However, since my collection will rarely exceed 10 or 12
items, would I be better of just using a regular List<MyObjectand
looping through the Names to find an object?

I know it's probably splitting hairs from a performance standpoint,
but I'm curious from a theoretical standpoint.

Jordan

Apr 9 '07 #3
Jordan,

Take a look at System.Collections.Specialized.HybridDictionary here:
http://msdn2.microsoft.com/en-us/lib...y_members.aspx

It implements IDictionary by using a ListDictionary while the collection is
small, and then switching to a Hashtable when the collection gets large.

Dave
Apr 10 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John Mudd | last post by:
I must be missing something here. It's clearly faster to lookup an item directly in a dictionary than to scan through a list. So when I have a large lookup table I always load it in the form of a...
14
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a...
6
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that expects a List<string>. I often do the following: ...
4
by: O.B. | last post by:
I need the ability to parse through the values of a Dictionary and remove certain ones depending on their attribute values. In the example below, an InvalidOperationException is thrown in the...
7
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
0
by: xpding | last post by:
Hello, I have a class MyEmbededList contains a generic dictionary, the value field is actually the MyEmbededList type as well. There is another class need to access and manipulate a list of...
11
by: Dan Upton | last post by:
This might be more information than necessary, but it's the best way I can think of to describe the question without being too vague. The task: I have a list of processes (well, strings to...
0
by: Gary Herron | last post by:
Dan Upton wrote: Yes. Create a list of keys, and loop through it: pids = procs_dict.keys() for pid in pids: if procs_dict.poll() != None # do the counter updates del procs_dict Then the...
18
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I've a question regarding dictionaries. I have an array elements that I created, and I'm trying to sort those elements into various sections. Here's the template of my...
1
by: sachin2 | last post by:
I am using 3 types of dictionaries. 1) Dictionary<string, string > d = new Dictionary<string, string>(); 2) Dictionary<string, List<string>> d = new Dictionary<string, List<string>>(); 3)...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.