The fastest data structure for checking property matching in a list of objects

I have a list that stores several objects. Each object has a property in the form of a variable.

I would like to check if any element in this list contains a specific property. Similar to Dictionary ContainsKey method. This data structure must contain an extremely large number of values, perhaps even millions, and therefore I would like to use a data structure that can check properties as quickly as possible.

Will the dictionary be the fastest for this job, or are there faster data structures?

EDIT:

Here is a quick, small example of what I would like to achieve:

Dictionary<string, Person> persons = new Dictionary<string, Person>(); //where string contains the Person name bool isPresent = persons.ContainsKey("Matt"); 
+6
source share
2 answers

It seems like you basically just need a HashSet<T> containing all the property values ​​- if you really want to know if it contains it or not.

For instance:

 var allNames = new HashSet<string>(people.Select(person => person.Name)); 
+6
source

It depends. If you can load data into a dictionary once, and then query it several times, then the dictionary is undoubtedly the fastest data structure possible. If multiple elements can have the same property value, you will need to create a Dictionary<TKey,List<TValue>> or use LINQ search.

However, if you need to download a list every time you request it, then there is no benefit from using the dictionary. You can determine the correct properties when loading a list, or if you query a database, then try to load only the data you need using the appropriate where clause.

0
source

Source: https://habr.com/ru/post/910411/


All Articles