Faster way to retreive records from collection by the passed key

V

Venkat

Hi,



I would like to have a multiple key dictionary to find a value
based on the given key.

Here is the example to demonstrate what I mean.



I have an Employee class



Public class Employee

{

Int m_EmpId;

String m_EmpName;

Float m_Salary;

String m_Department;

}



Like this I have a collection of objects and I need to retrieve a record
based on the passed Employee id or employee name. Looping through an
arraylist is too slow. And there is no dictionary available to combine both
keys. So I need to maintain two dictionary classes if I want it faster.



Is there any better way to find the record faster?



Thanks in advance.



Cheers,

Venkat
 
D

Dustin Campbell

Like this I have a collection of objects and I need to retrieve a
record based on the passed Employee id or employee name. Looping
through an arraylist is too slow. And there is no dictionary available
to combine both keys. So I need to maintain two dictionary classes if
I want it faster.

Is there any better way to find the record faster?

Well, you could just add the same item to a Hashtable twice with different
keys.

Best Regards,
Dustin Campbell
Developer Express Inc.
 
S

ssamuel

That's probably the easiest way to do it. Probably best write it with
two Hashtables (Dictionary<>s in 2.0), and leave it like that until you
get to performance optimization.

If you have *a lot* of data, it may not be the most efficient. If you
find that you're constrained, most likely by memory but also maybe
slightly by performance, you could try one of the following:

1. Create your own collection data structure that contains a master
collection in a data structure, and parallel searchable data structures
as the indices. It's up to you to guarantee referential integrity
between the indices and the master structure. This is complicated but
will give you the best performance if you design it right.

2. Use some sort of DataSet. Various project parameters will determine
if a typed DataSet is appropriate. You'll be able to perform selects
against it based on any key. I've never used them, but I believe you
may even be able to add indices. You get the generic DataSet overhead,
but it's rather efficient in most cases.


Stephan
 

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

Top