Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Custom Generic Collection faster with objects than List

I'm iterating through a List<> to find a matching element. The problem is that object has only 2 significant values, Name and Link (both strings), but has some other values which I don't want to compare.

I'm thinking about using something like HashSet (which is exactly what I'm searching for -- fast) from .NET 3.5 but target framework has to be 2.0. There is something called Power Collections here: http://powercollections.codeplex.com/, should I use that?

But maybe there is other way? If not, can you suggest me a suitable custom collection?

like image 658
Kaminari Avatar asked Dec 21 '25 07:12

Kaminari


1 Answers

In .NET 2.0 instead of a HashSet<T> you can use a Dictionary<K, V>.

Dictionary uses the hash code to perform key lookups so it has similar performace to the HashSet. There are at least two approaches:

  • Create a custom class or struct containing the Name and Link and use that as the key in the dictionary, and put the object as the value.
  • Store the entire object as the key and provide a custom equality comparer that only looks at the Name and Link member, and set the value to null.

The second method is very similar to how you would use a HashSet if it were available.

like image 189
Mark Byers Avatar answered Dec 23 '25 19:12

Mark Byers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!