Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest data structure to check if a property within a list of objects matches

I've got a list which stores a number of objects. Each object has a property in the form of a variable.

I'd like to be able to check if any of the items in this list contain a certain property. Similar to the Dictionary's ContainsKey method. This data structure is to hold an extremely large amount of values, possibly even millions and I would thus like to use a data structure which can check the properties as fast as possible.

Would Dictionary be the fastest for this job, or are there faster data structures?

EDIT:

Here's a quick, small example of what I'd like to achieve:

Dictionary<string, Person> persons = new Dictionary<string, Person>(); //where string contains the Person's name

bool isPresent = persons.ContainsKey("Matt");
like image 944
Dot NET Avatar asked Mar 10 '12 17:03

Dot NET


1 Answers

It sounds like you basically just need a HashSet<T> containing all the property values - assuming you really just want to know whether it's contained or not.

For example:

var allNames = new HashSet<string>(people.Select(person => person.Name));
like image 54
Jon Skeet Avatar answered Nov 17 '22 03:11

Jon Skeet