Hash Tables

W

William Ryan

Both.

A hash table is derived from the System.Collections namespace. This is from
MSDN which is a good example on how to use one...
The first value is the Key, the second is the Value. I'm not sure what you
want to do so I gave a generic answer, but if you need something specific,
let me know and I'll do what I can to help you.

Cheers.

Bill
Hashtable phones = new Hashtable();
// Add items.
phones.Add("John", "123-4567");
phones.Add("Enju", "351-8765");
phones.Add("Molly", "221-5678");
phones.Add("James", "010-4077");
phones.Add("Nelly", "110-5699");
phones.Add("Leah", "922-5699");

// Iterate through the collection.
Console.WriteLine("Name\t\tNumber");
foreach (string name in phones.Keys)
{
Console.WriteLine(name +"\t"+ phones[name]);
 
L

Lou

I need to use collections, like class collections and just some generic
collections and am
not sure which is better, more efficiant etc. I noticed there are
"Collections"
"Dictionary"
"Hash Table"
and even some more.

why so many collections?
is there a reason to use one over another?

Thanks...

William Ryan said:
Both.

A hash table is derived from the System.Collections namespace. This is from
MSDN which is a good example on how to use one...
The first value is the Key, the second is the Value. I'm not sure what you
want to do so I gave a generic answer, but if you need something specific,
let me know and I'll do what I can to help you.

Cheers.

Bill
Hashtable phones = new Hashtable();
// Add items.
phones.Add("John", "123-4567");
phones.Add("Enju", "351-8765");
phones.Add("Molly", "221-5678");
phones.Add("James", "010-4077");
phones.Add("Nelly", "110-5699");
phones.Add("Leah", "922-5699");

// Iterate through the collection.
Console.WriteLine("Name\t\tNumber");
foreach (string name in phones.Keys)
{
Console.WriteLine(name +"\t"+ phones[name]);
}"Lou said:
is a Hash Table equivalent to a VB6 Collection or Dictionary object?

-Lou
 
W

William Ryan

Dictionary's and hashtables are collections. So are arraylists. Typically,
you'd use a collection and strongly type it so you enforce what it's members
are. A hash Table for instace, allows you to effiiciently find members
without having to iterate through them. ArrayLists for instance necessitate
that you iterate through them to find a given member. Hash Tables don't let
you iterate the same way. You could store whatever object in most of those
structures...they are more alike then different.

If You need to use collections, any of those will techincally meet the
requirement. Think of it like this. If the boss said I need a Person to
accomplish something, a Canadian, American or Mexican might be better suited
to a task (for instance, a Mexican might be better at Spanish interpretation
or languge specific task) but all three of the people are in fact 'Person's.
So you would meet the requirement whichever you chose.
Lou said:
I need to use collections, like class collections and just some generic
collections and am
not sure which is better, more efficiant etc. I noticed there are
"Collections"
"Dictionary"
"Hash Table"
and even some more.

why so many collections?
is there a reason to use one over another?

Thanks...

William Ryan said:
Both.

A hash table is derived from the System.Collections namespace. This is from
MSDN which is a good example on how to use one...
The first value is the Key, the second is the Value. I'm not sure what you
want to do so I gave a generic answer, but if you need something specific,
let me know and I'll do what I can to help you.

Cheers.

Bill
Hashtable phones = new Hashtable();
// Add items.
phones.Add("John", "123-4567");
phones.Add("Enju", "351-8765");
phones.Add("Molly", "221-5678");
phones.Add("James", "010-4077");
phones.Add("Nelly", "110-5699");
phones.Add("Leah", "922-5699");

// Iterate through the collection.
Console.WriteLine("Name\t\tNumber");
foreach (string name in phones.Keys)
{
Console.WriteLine(name +"\t"+ phones[name]);
}"Lou said:
is a Hash Table equivalent to a VB6 Collection or Dictionary object?

-Lou
 
L

Lou

Thanks William, that was well said. You'd make a great teacher.
-Lou

William Ryan said:
Dictionary's and hashtables are collections. So are arraylists. Typically,
you'd use a collection and strongly type it so you enforce what it's members
are. A hash Table for instace, allows you to effiiciently find members
without having to iterate through them. ArrayLists for instance necessitate
that you iterate through them to find a given member. Hash Tables don't let
you iterate the same way. You could store whatever object in most of those
structures...they are more alike then different.

If You need to use collections, any of those will techincally meet the
requirement. Think of it like this. If the boss said I need a Person to
accomplish something, a Canadian, American or Mexican might be better suited
to a task (for instance, a Mexican might be better at Spanish interpretation
or languge specific task) but all three of the people are in fact 'Person's.
So you would meet the requirement whichever you chose.
Lou said:
I need to use collections, like class collections and just some generic
collections and am
not sure which is better, more efficiant etc. I noticed there are
"Collections"
"Dictionary"
"Hash Table"
and even some more.

why so many collections?
is there a reason to use one over another?

Thanks...

William Ryan said:
Both.

A hash table is derived from the System.Collections namespace. This
is
from
MSDN which is a good example on how to use one...
The first value is the Key, the second is the Value. I'm not sure
what
you
want to do so I gave a generic answer, but if you need something specific,
let me know and I'll do what I can to help you.

Cheers.

Bill
Hashtable phones = new Hashtable();
// Add items.
phones.Add("John", "123-4567");
phones.Add("Enju", "351-8765");
phones.Add("Molly", "221-5678");
phones.Add("James", "010-4077");
phones.Add("Nelly", "110-5699");
phones.Add("Leah", "922-5699");

// Iterate through the collection.
Console.WriteLine("Name\t\tNumber");
foreach (string name in phones.Keys)
{
Console.WriteLine(name +"\t"+ phones[name]);
is a Hash Table equivalent to a VB6 Collection or Dictionary object?

-Lou
 
R

Richard Grimes [MVP]

Lou said:
I need to use collections, like class collections and just some
generic collections and am
not sure which is better, more efficiant etc. I noticed there are
"Collections"
"Dictionary"
"Hash Table"
and even some more.

why so many collections?
is there a reason to use one over another?

There are several criteria. The most important is how you will access the
data in the collection. Do you want serial access to read data (ie start at
the first item, then the second, then the third, etc) or do you want random
access (ie read item 13, read item 5, read item 22, etc)? When you write
data to the collection will you write them in the order that they will be
read (a Queue is good for this) or do you know the exact position that you
want the item in the collection (ie write item 13, then item 44 etc)?

A class that supports IEnumerable is designed to allow serial access so that
you repeatedly read a value from the collection until the end is reached. A
class that supports ICollection has the IEnumerable features as well as a
Count to determine the number of items in the collection. IDictionary means
that items can be accessed by some index object that you specify as a key
(for example the SSN is a key for a Hashtable of employee names). A class
with IList means that the collection has ransom access using the position of
the item as the index.

Do you know the size of the collection when you create it, or do you want
the collection to grow as more items are inserted? Do you want to rearrange
the order of the items after you have inserted them into the collection? Do
you want to do a search for an item with a specific value? Finally, what
type item will be in the collection - will they be the same type (eg all
strings) or will you put different types of items in the collection? If you
want to create an array of value types (eg integers or real numbers) then it
is better to use an array because the items will be 'boxed' when they are
put in the collection which means that a new object is created for each item
and this will increase your memory usage (and there are other issues with
boxing).

- Array is fixed size, allows random access and allows searches and
reordering. An array is typed.
- ArrayList is variable size, allows randowm access, searching and
reordering and is untyped. It can be used to create an array from the items
in the collection.
- Hashtable is not ordered by the order that you inserted the items, it is
variable size, allows random access. The time taken to get an item in a
Hashtable is only as good as the hash.
- SortedList contain key-value pairs, sorted according to the key, hence
inserting items takes time. Supports searches by key or value.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top